【发布时间】:2018-09-14 07:13:15
【问题描述】:
此代码在 MSVC 上编译失败,因为 static_assert 失败:
template<class MyType>
struct Test {
static_assert(MyType(5) != MyType(6), "fails");
};
见:https://godbolt.org/z/vUSMHu
知道 MSVC 如何在不知道 MyType 是什么的情况下对此进行评估?
更加晦涩:
template<class MyType>
struct Test {
static_assert(MyType(5) == MyType(6), "succeeds");
static_assert(!(MyType(5) == MyType(6)), "fails");
};
见:https://godbolt.org/z/3631tu
实例化它(即给 MyType 一个类型)也无济于事:
template<class MyType>
struct Test {
static_assert(MyType(5) != MyType(6), "still fails");
};
Test<int> variable;
见:https://godbolt.org/z/yxF4h0
或者更复杂一点:https://godbolt.org/z/68g6yO
【问题讨论】:
-
因为它适用于 clang 和 gcc 并且它不是一个 hack,所以可以安全地假设它是编译器错误
-
在 VS2017 15.8.4 上没有复制。请注意,godbolt VS 编译器已经过时了。 @Evg 没有 MSVS 2018,它是 VS 2017 或 VS 2019(即将发布)
-
@VTT,抱歉,最新的 MSVS 2017 预览版。
标签: c++ visual-studio visual-c++ visual-studio-2017 static-assert