C 11中ConstExpr对象中的可变成员

C 11中ConstExpr对象中的可变成员,第1张

概述在C 14标准(ISO / IEC 14882:2014)中,第5.19节第2段(强调我的)增加了“不可变”一词: A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would 在C 14标准(ISO / IEC 14882:2014)中,第5.19节第2段(强调我的)增加了“不可变”一词:

A conditional-Expression e is a core constant Expression unless the evaluation of e,following the rules of the abstract machine (1.9),would evaluate one of the following Expressions:

[…] an lvalue-to-rvalue conversion (4.1) unless it is applIEd to […] a non-volatile glvalue that refers to a non-volatile object defined with constexpr,or that refers to a non-mutable sub-object of such an object,or

因此,此代码在C 14中不正确:

class A {    public:        mutable int x;};int main(){    constexpr A a = {1};    constexpr int y = a.x;    return 0;}

但是,它在C 11中是否正确?

这是缺陷报告(CD3)1405,他们建议添加不可变:

Currently,li@R_502_6704@l class types can have mutable members. It is not clear whether that poses any particular problems with constexpr objects and constant Expressions,and if so,what should be done about it.

所以我会说这是正确的C 11代码.然而,我用-std = c 11尝试了Clang和GCC,并且都输出了一个错误,表示在常量表达式中不允许使用可变变量.但是这个约束是在C 14中添加的,它不在C 11中.

有谁知道C 11中的代码是否正确?

另见缺陷报告(CD3)1428.

解决方法 它是C 11缺陷报告,然后C 11需要修复.只有具有DR,接受,DRWP和WP状态的问题不属于C国际标准的一部分.

符合C 11的编译器必须实现该DR.

例如,由于DR 1579,这对示例发生了变化:

> gcc 6.1.0
> gcc 4.9.3

此示例取自:Why this C++ program gives different output in C++11 & C++14 compilers

总结

以上是内存溢出为你收集整理的C 11中ConstExpr对象中的可变成员全部内容,希望文章能够帮你解决C 11中ConstExpr对象中的可变成员所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1228796.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存