MSVC 错误?未找到受约束函数的重载成员

Das*_*kie 7 c++ visual-studio language-lawyer c++20

此代码无法使用 MSVC 19.27.29112.0 进行编译,但适用于 GCC 10.2.0。在这两种情况下都启用了 C++20:

template <typename T>
struct Blah {
    void blah() requires (sizeof(T) == 4);
};

template <typename T>
void Blah<T>::blah() requires (sizeof(T) == 4) {}

int main() {
    Blah<int> b;
    b.blah();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误 C2511:“void Blah::blah(void)”:在“Blah”中找不到重载的成员函数

仅当requires依赖于类的模板类型时才会发生错误。例如,requires (sizeof(int) == 4)工作正常。转换blah()为模板化函数并执行类似的操作requires (sizeof(U) == 4)也有效。

谁能确认这是一个编译器错误?

dxi*_*xiv 5

评论太长了。)这确实看起来像一个错误,我建议你正式报告它

奇怪的是,下面的工作,而不是。

template <typename T>
struct Blah {
    enum { sizeofT = sizeof(T) };  // or: static const size_t sizeofT = sizeof(T);
                                   // or: static constexpr size_t sizeofT = sizeof(T);
    void blah() requires (sizeofT == 4);
};

template <typename T>
void Blah<T>::blah() requires (sizeofT == 4) {}

int main() {
    Blah<int>().blah();       // ok
    Blah<float>().blah();     // ok

//  Blah<short>().blah()      // c7500: 'blah': no function satisfied its constraints
//  Blah<double>().blah();    // c7500: 'blah': no function satisfied its constraints

    return 0;
}
Run Code Online (Sandbox Code Playgroud)