gpt4 book ai didi

c++ - 模板中的第二个lambda函数导致编译错误(智能感知无法检测到问题)-错误C2988

转载 作者:行者123 更新时间:2023-12-03 07:22:14 26 4
gpt4 key购买 nike

我使用的是最新版本的C++(猜想这是 visual studio enterprise 16.8 中可用的 C++ 17 C++ 20 之间的某种错误(但此错误也发生在以前的版本中)。
如果从模板中排除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, SquareMatrixPrint<U> pf, SquareMatrixInput<U> fi>
const SquareMatrix<T, print_func, input_func>& operator=(const SquareMatrix<U, pf, 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) { cout << item << (latest ? '\n' : '\t'); },
[](bool latest) { int temp; cin >> temp; return temp; } > sq(4);

return 0;
}

最佳答案

我认为这是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 {
//...

关于c++ - 模板中的第二个lambda函数导致编译错误(智能感知无法检测到问题)-错误C2988,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64777578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com