这个非常简单的代码代码在GCC 6.0中给出了一个错误:
template<class T>
struct S {
// error: cannot convert 'T' to 'const int' in initialization
static const int b = T{};
};
int main() {
}
奇怪的是,如果我改用常规大括号 (T()
),则代码可以编译。这是一个错误吗?代码在 clang 中编译良好。
最佳答案
T()
起作用的原因是编译器将其解释为不带参数的函数声明。编译将通过显式转换完成:
static const int b = (const int) T{};
关于c++ - 使用大括号将临时值初始化为静态数据成员的初始化程序会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677822/