MSVC 错误?找不到受约束函数的重载成员
MSVC Bug? Overloaded member not found for constrained function
此代码无法使用 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;
}
error C2511: 'void Blah::blah(void)': overloaded member function not found in 'Blah'
仅当 requires
依赖于 class 的模板类型时才会出现此错误。例如,requires (sizeof(int) == 4)
工作正常。将 blah()
转换为模板函数并执行类似 requires (sizeof(U) == 4)
的操作也有效。
谁能确认这是一个编译器错误?
(评论太长。)这看起来确实是一个错误,我正式建议你 report it。
奇怪的是,下面的工作,而不是。
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;
}
显然,gcc 和 clang 知道您的代码中存在重载 (here)。但是,我认为与 gcc 和 clang 相比,MS 编译器团队会在不同的阶段对内存初始化模块进行流水线处理。我 (kind-of) 推测这是因为以下代码有效(即,在结构模板本身中为 blah() 函数初始化和创建内存):
template<typename T>
struct Blah {
void blah() requires (sizeof(T) == 4) {};
};
int main() {
Blah<int> b;
b.blah();
}
此代码无法使用 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;
}
error C2511: 'void Blah::blah(void)': overloaded member function not found in 'Blah'
仅当 requires
依赖于 class 的模板类型时才会出现此错误。例如,requires (sizeof(int) == 4)
工作正常。将 blah()
转换为模板函数并执行类似 requires (sizeof(U) == 4)
的操作也有效。
谁能确认这是一个编译器错误?
(评论太长。)这看起来确实是一个错误,我正式建议你 report it。
奇怪的是,下面的工作,而不是。
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;
}
显然,gcc 和 clang 知道您的代码中存在重载 (here)。但是,我认为与 gcc 和 clang 相比,MS 编译器团队会在不同的阶段对内存初始化模块进行流水线处理。我 (kind-of) 推测这是因为以下代码有效(即,在结构模板本身中为 blah() 函数初始化和创建内存):
template<typename T>
struct Blah {
void blah() requires (sizeof(T) == 4) {};
};
int main() {
Blah<int> b;
b.blah();
}