为什么我不能使用 msvc 在模板 class 中声明静态 constexpr 变量?
Why can't I declare a static constexpr variable in a template class using msvc?
我有一个模板 class,我想声明一个与 class 相同类型的静态 constexpr 变量。使用 gnu 编译器它工作得很好,但是
Microsoft Visual Studio,它不会编译。我是不是做错了什么,开玩笑说 gnu 编译器对我很好,还是 Microsoft 编译器出了问题?
我知道我可以修复它,为做同样事情的函数更改变量,但我很好奇。
template <typename T>
constexpr T One() noexcept { return static_cast<T>( 1 ); }
template <typename T>
struct Test {
T val;
static constexpr Test example{ One<T>() }; // compiles only with gnu
static constexpr Test Example() { return Test{ One<T>() }; } // compiles with both gnu and microsoft
};
给定的错误 (Visual Studio 2017) 是:
error C2017 : use of undefined type 'Test'
在最后一个 }
之前,您的 Test
模板类型不完整。
这里的 very similar question 添加了第三个编译器。如您所见,答案是 VC++ 并且 clang 遵守标准,而 gcc 不遵守。
我有一个模板 class,我想声明一个与 class 相同类型的静态 constexpr 变量。使用 gnu 编译器它工作得很好,但是 Microsoft Visual Studio,它不会编译。我是不是做错了什么,开玩笑说 gnu 编译器对我很好,还是 Microsoft 编译器出了问题? 我知道我可以修复它,为做同样事情的函数更改变量,但我很好奇。
template <typename T>
constexpr T One() noexcept { return static_cast<T>( 1 ); }
template <typename T>
struct Test {
T val;
static constexpr Test example{ One<T>() }; // compiles only with gnu
static constexpr Test Example() { return Test{ One<T>() }; } // compiles with both gnu and microsoft
};
给定的错误 (Visual Studio 2017) 是:
error C2017 : use of undefined type 'Test'
在最后一个 }
之前,您的 Test
模板类型不完整。
这里的 very similar question 添加了第三个编译器。如您所见,答案是 VC++ 并且 clang 遵守标准,而 gcc 不遵守。