简体   繁体   English

vs 2015 constexpr 变量非常量但在 vs 2019 上很好?

[英]vs 2015 constexpr variable non constant but fine on vs 2019?

I have a simple constexpr class (using c++17)我有一个简单的 constexpr 类(使用 c++17)

struct foo {
   float x, y;

   // ill formed constexpr in vs 2015
   foo() {}

   constexpr foo(float x, float y) : x(x), y(y) {
   }

};

constexpr auto bar() {
    return foo(4.0, 5.0);
}

int main() {
    auto f = bar();
}

This is illformed bc of the non constexpr default constructor on 2015. But vs 2019 doesnt report a problem.这是 2015 年非 constexpr 默认构造函数的病态 bc。但是 vs 2019 没有报告问题。

According to this reference page "all selected ctors" must be constexpr.根据这个参考页面“所有选定的ctors”必须是constexpr。 I assume the word "selected" means "used" and even though default ctor is unused, 2015 wont eval into a constexpr until I make the default ctor constexpr.我假设“selected”这个词的意思是“使用”,即使默认ctor未使用,2015年也不会评估为constexpr,直到我创建默认ctor constexpr。

Other specs are worded differently but I could not make clear of them either.其他规格的措辞不同,但我也无法弄清楚。 Also most constexpr examples use the keyword everywhere.大多数 constexpr 示例也到处使用关键字。 An example of selective constexpr use would be nice.一个选择性使用 constexpr 的例子会很好。

Latest GNU ans Clang behave like vs 2019 but Im not convinced that this was an oversight or bug in vs 2015.最新的 GNU ans Clang 表现得像 vs 2019,但我不相信这是 vs 2015 中的疏忽或错误。

So what is it?那是什么? Should all ctors be constexpr or is visual studio 2019 correct here?所有的ctor都应该是constexpr还是visual studio 2019在这里正确?

There has never been a requirement that all constructors of a literal type must be constexpr .从来没有要求文字类型的所有构造函数都必须是constexpr The requirement has only been that all functions which get invoked in a constant expression context must be able to be constant expressions (and thus must be declared constexpr ).要求只是在常量表达式上下文中调用的所有函数必须能够是常量表达式(因此必须声明为constexpr )。

Note that older versions of Visual Studio did not do a good job of implementing constexpr correctly.请注意,旧版本的 Visual Studio 在正确实现constexpr做得不好。

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

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