繁体   English   中英

MSVC constexpr 编译器错误?

[英]MSVC constexpr Compiler Bug?

环境: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);
}

这是输出:

error C2131: expression did not evaluate to a constant

message : failure was caused by attempting to access a member on an object of dynamic type 'mixin<thing>' in which the member is not defined

message : see usage of 'thing::value_'

错误指的是main()的第二个static_assert ,两条消息指的是thing内部的value()成员函数。

似乎do_mixin_thing()static_cast是导致问题的原因。 我尝试通过constexpr T const& self() const { return static_cast<T const&>(*this); }将演员表添加到mixin constexpr T const& self() const { return static_cast<T const&>(*this); }但错误仍然存​​在。

这已在 Visual Studio 2019 版本 16.9 中修复。 演示: https : //gcc.godbolt.org/z/4Y94co6hW

感谢错误报告者: https : //developercommunity.visualstudio.com/t/valid-static-cast-causing-a-constexpr-failure/908077

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM