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

标签 c++ templates visual-c++ lambda c++17

我使用的是最新版本的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/

相关文章:

c++ - 如何在 Qt Designer 中使小部件自动拉伸(stretch)到父小部件的大小

c++在据称不应该有好处的地方 move 语义

c++ - srand(time(NULL)) 不好吗?

ruby-on-rails - rails : render partial with selected controller and action in view

c++ - 如何使用 Visual C++ 中的 Delphi 寄存器调用约定调用函数?

c++ - 如何制作一个宏来在for循环中生成代码?

c++ - 为具有不同模板参数的模板部分特化模板类

java - 用于基于 Java 模板的字符串构造的工具

visual-c++ - 如何向我的 C# 应用程序公开 C++ 方法?

c++ - 我的 C++ 程序在计算欧拉数的级数时遇到问题