简体   繁体   English

constexpr 比 const 更“恒定”吗?

[英]Is a constexpr more “constant” than const?

The C++ Programming Language Fourth Edition - Bjarne Stroustrup: (emphasis mine) C++ 编程语言第四版 - Bjarne Stroustrup:(强调我的)

2.2.3. 2.2.3. Constants常数

In a few places, constant expressions are required by language rules (eg, array bounds (§2.2.5, §7.3), case labels (§2.2.4, §9.4.2), some template arguments (§25.2), and constants declared using constexpr).在一些地方,语言规则需要常量表达式(例如,数组边界(第 2.2.5 节、第 7.3 节)、案例标签(第 2.2.4 节、第 9.4.2 节)、一些模板参数(第 25.2 节)和使用 constexpr 声明的常量)。 In other cases, compile-time evaluation is important for performance.在其他情况下,编译时评估对性能很重要。 Independently of performance issues, the notion of immutability (of an object with an unchangeable state) is an important design concern (§10.4).独立于性能问题,不变性(具有不可更改状态的对象)的概念是一个重要的设计问题(第 10.4 节)。

It seems that Stroustrup is suggesting here that constexpr ensures immutability of an object better than a traditional const declaration.似乎 Stroustrup 在这里建议constexpr比传统的const声明更好地确保对象的不变性。 Is this correct?这样对吗? Are there ways in which constexpr can be more secure/less volatile than const , or does Stroustrup simply mean that since there are ways to use constexpr that are not supported with const (see Is constexpr really needed? ), in those cases immutability can be ensured using constexpr ?有没有办法让constexprconst更安全/更不稳定,或者 Stroustrup 只是意味着因为有一些方法不支持const支持使用constexpr (请参阅是否真的需要 constexpr? ),在这些情况下不变性可以是使用constexpr确保?

He states in the beginning of the section:他在本节开头说:

C++ supports two notions of immutability C++ 支持两种不变性概念

and he lists const and constexpr , I don't believe he is attempting to say that constexpr ensures immutability better than const they just have different features, although I admit the fact the sentence cites section 10.4 Constant Expressions does seem to imply that, that interpretation is inconsistent with the rest of the text.并且他列出了constconstexpr ,我不相信他试图说constexprconst更好地确保不变性,它们只是具有不同的功能,尽管我承认该句子引用了第10.4常量表达式的事实似乎暗示,该解释与正文的其余部分不一致。

A variable that is const is immutable in that scope but may not be const in the larger scope( for example a const reference parameter to a function ) and that is perhaps a subtle distinction he is attempting to make, he says that const :一个const变量在该范围内是不可变的,但在更大范围内可能不是const例如一个函数的 const 引用参数),这可能是他试图做出的一个微妙的区别,他说const

is used primarily to specify interfaces主要用于指定接口

whereas constexpr :constexpr

This is used primarily to specify constants, to allow placement of data in read-only memory这主要用于指定常量,以允许将数据放置在只读存储器中

Any variable that is constexpr should be evaluated at compile time and thus usable where constant expressions are required whereas a variable that is passed as const to a function does not have to be const outside that scope.任何constexpr变量都应该在编译时求值,因此可以在需要常量表达式的情况下使用,而作为const传递给函数的变量不必在该范围之外是const

Sure you can cast away constness using const_cast but attempting to modify a const object is undefined behavior and so it is no less immutable than constexpr in that sense, from the draft C++11 standard section 7.1.6.1 The cv-qualifiers :当然,你可以抛弃使用常量性const_cast ,但试图修改一个const对象是不确定的行为,所以它是不低于一成不变constexpr在这个意义上说,从草案C ++ 11标准的部分7.1.6.1的CV-预选赛

any attempt to modify a const object during its lifetime (3.8) results in undefined behavior任何在其生命周期 (3.8) 期间修改 const 对象的尝试都会导致未定义的行为

Jonathan Wakely notes that a constexpr variable like const variables can have a mutable member but that member can not be used in a constant expression. Jonathan Wakely 指出,像const变量这样的constexpr变量可以有一个 可变成员,但该成员不能用于常量表达式。

Note that a constexpr variable is also const , from the draft C++11 standard section 7.1.5 The constexpr specifier :请注意, constexpr变量也是const ,来自 C++11 标准草案第7.1.5constexpr 说明符

A constexpr specifier used in an object declaration declares the object as const.对象声明中使用的 constexpr 说明符将对象声明为 const。

const dosn't ensure bitwise constness, eg as classes can have mutable members (typical example would be a private mutex for internal synchronization) and you can const_cast away the constness from a pointer. const确保按位const ,例如,类可以具有可变成员(典型示例是用于内部同步的私有互斥锁),并且您可以const_cast远离指针的常量。

constexpr declares a constant variable or function that can be calculated at compile time, which implies some restrictions on what the object can be, but I believe, the keyword itself doesn't provide any additional guarantees during runtime compared to const . constexpr声明了一个可以在编译时计算的常量变量或函数,这意味着对对象可以是什么有一些限制,但我相信,与const相比,关键字本身在运行时不提供任何额外的保证。 See also this discussion.又见这个讨论。

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

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