MSVC 在不知道类型的情况下评估上下文(和错误)

MSVC evaluating context (and erroring) without knowing types

本文关键字:上下文 错误 评估 情况下 不知道 类型 MSVC      更新时间:2023-10-16

此代码在 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

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

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