环境:VS 2019,v16.4.3 w/c++17 + 最新开关
以下代码标准是否正确,还是我做错了什么?它使用最新的 gcc/clang 编译器可以正常编译,但在 MSVC 上失败。(请参阅下面的错误消息)
template<typename T>
struct mixin {};
struct thing : mixin<thing>
{
constexpr explicit thing(int value) : value_(value) {}
constexpr int value() const { return value_; }
private:
int value_ = 0;
};
template<typename T>
constexpr auto do_mixin_thing(mixin<T> const& m)
{
return static_cast<T const&>(m).value() * 10;
}
int main()
{
constexpr thing t1{ 10 };
// this works
static_assert(t1.value() == 10);
// this fails
static_assert(do_mixin_thing(t1) == 100);
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
error C2131: expression …
Run Code Online (Sandbox Code Playgroud)