【问题标题】:MSVC evaluating context (and erroring) without knowing typesMSVC 在不知道类型的情况下评估上下文(和错误)
【发布时间】: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


【解决方案1】:

是的,这个奇怪的错误发生在 MSVC 19.10 中,但在 MSVC 19.14 及更高版本中已经无法重现。演示:https://gcc.godbolt.org/z/635Mxdazd

因此可以合理地假设它只是一个编译器错误。

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 2016-05-18
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多