如何解决模板中的第二个lambda函数导致编译错误智能感知无法检测到问题-错误C2988
我正在使用Visual Studio Enterprise中可用的最新版本的C ++(猜测是介于 C ++ 17 和 C ++ 20 之间的某个东西) 16.8 (但此错误也发生在以前的版本中)。
如果从模板中排除SquareMatrixInput<T> input_func = [](bool latest) { T temp; cin >> temp; return temp; }
,则以下部分代码可以正常工作(当然,我也从类代码中排除SquareMatrixInput<T>
)。
Intellitrace满意,但是编译器发誓。
编译器的输出:
error C2958: the left brace '{' found at 'my_code.cpp' was not matched correctly
error C2988: unrecognizable template declaration/deFinition
error C2059: Syntax error: '>'
error C2988: unrecognizable template declaration/deFinition
error C2059: Syntax error: 'return'
error C2988: unrecognizable template declaration/deFinition
error C2059: Syntax error: '}'
error C2143: Syntax error: missing ';' before '}'
error C7568: argument list missing after assumed function template 'SquareMatrix'
代码:
#include <iostream>
using namespace std;
template<typename T>
using SquareMatrixPrint = void (*)(const T& item,bool latest);
template<typename T>
using SquareMatrixInput = T (*)(bool latest);
template<typename T = int,SquareMatrixPrint<T> print_func = [](const T &item,bool latest) { cout << item << (latest ? '\n' : '\t'); },SquareMatrixInput<T> input_func = [](bool latest) { T temp; cin >> temp; return temp; } >
class SquareMatrix {
uint8_t grade;
uint16_t size;
T *items;
public:
explicit SquareMatrix(uint8_t grade) {
if (grade < 2) {
throw new exception();
}
this->grade = grade;
size = grade * grade;
items = new T[size]{};
}
const T *operator[] (uint8_t i) const {
return items + i * grade;
}
template <typename U,SquareMatrixPrint<U> pf,SquareMatrixInput<U> fi>
SquareMatrix(const SquareMatrix<U,pf,fi> &sqm) {
grade = sqm.GetGrade();
size = grade * grade;
items = new T[size];
T *citem = items;
for (uint8_t i = 0,j; i != grade; ++i) {
for (j = 0; j != grade; ++j) {
*citem = sqm[i][j];
++citem;
}
}
}
template <typename U,SquareMatrixInput<U> fi>
const SquareMatrix<T,print_func,input_func>& operator=(const SquareMatrix<U,fi> &sqm) {
if (this == &sqm) return *this;
if (grade != sqm.GetGrade()) {
grade = sqm.GetGrade();
size = grade * grade;
delete[] items;
items = new T[size];
}
T *citem = items;
for (uint8_t i = 0,j; i != grade; ++i) {
for (j = 0; j != grade; ++j) {
*citem = sqm[i][j];
++citem;
}
}
return *this;
}
~SquareMatrix() {
delete[] items;
}
};
int main() {
SquareMatrix<> sq(4);
return 0;
}
int main() {
SquareMatrix<int,[](const int &item,[](bool latest) { int temp; cin >> temp; return temp; } > sq(4);
return 0;
}
解决方法
您好:这是由于Visual C ++中的错误引起的,原因是上述问题-编译器将'>>'作为模板参数列表的终止符。该错误已修复,应在Visual C ++ 2019的下一个更新中显示。
,使用make <LIBNAME>_DIR=/path/to/lib ...
解析模板参数中的问题
cin >> temp;
被(错误地)解释为模板的右尖括号。
您必须加上括号:
>>
我认为这是Visual Studio编译器中的错误(我可以在Community Edition 2019中进行复制)。 Clang-cl的代码没有问题。
问题似乎是>>
中的cin >> temp;
运算符使编译器感到困惑,并且可以通过将该操作括在圆括号中来进行修复,如下所示:
template<typename T = int,SquareMatrixPrint<T> print_func = [](const T& item,bool latest) { cout << item << (latest ? '\n' : '\t'); },SquareMatrixInput<T> input_func = [](bool) { T temp; (cin >> temp); return temp; } >
class SquareMatrix {
//...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。