【发布时间】:2019-10-11 07:40:28
【问题描述】:
我有一个模板类,我想声明一个与该类相同类型的静态 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
};
给定的错误(Visal Studio 2017)是:
错误 C2017:使用未定义类型“测试”
【问题讨论】:
-
哪个确切版本的 MSVC?
-
我之前也在编译器资源管理器上尝试过,即使是 19.20 版也无法编译
-
因为 GNU 太宽容了!!
example应该有返回声明! -
但它是一个变量...
标签: c++ visual-studio templates gnu constexpr