是一个constexpr更“恒定”比const? [英] Is a constexpr more "constant" than const?
问题描述
常数
在某些地方,语言规则需要常量表达式
数组边界(§2.2.5,§7.3),情况标签(§2.2.4,§9.4.2),一些
模板参数(§25.2)和使用constexpr声明的常量。
在其他情况下,编译时评估对性能很重要。
独立于性能问题,不变性(具有不可改变状态的对象)的概念是重要的设计问题
(§10.4)。
看来Stroustrup在这里建议 constexpr
确保对象的不可变性比传统的 const
声明更好。它是否正确?是否有方法, constexpr
可以比 const
更安全/不易变,或者Stroustrup简单意味着是使用 const
不支持的 constexpr
的方法(请参阅 constexpr真的需要吗? ),在这些情况下,可以使用 constexpr
他在本节开头说:
C ++支持两个不变性的概念
,他列出了 const 和 constexpr ,我不相信他试图说, constexpr 确保不变性比 const 他们只是有不同的功能,我承认句子引用 10.4
常量表达式似乎暗示,该解释与文本的其余部分不一致。
一个变量 const
在该范围内是不可变的,但不能 const
在更大的范围(,例如一个const引用参数的函数),这也许是一个微妙的区别,他试图做,他说 const
:
主要用于指定接口
而 constexpr
:
主要用于指定常量,以允许将数据放置在只读存储器中
任何变量 constexpr
应在编译时进行计算,因此可用于需要常量表达式,而作为 const
传递给函数的变量不必 const
。
当然你可以使用 const_cast
但是试图修改一个 const
对象是未定义的行为,所以它不是不变的 constexpr
在这个意义上,从 draft C ++ 11 standard section 7.1.6.1
任何在其生命周期内修改const对象的尝试都会导致未定义的行为
Jonathan Wakely注意到 constexpr 变量(如 const 变量)可以有可变成员,但该成员不能在常量表达式中使用。
请注意, < em c ++ c>>< em> constexpr 变量也是 const
: :
The C++ Programming Language Fourth Edition - Bjarne Stroustrup: (emphasis mine)
2.2.3. Constants
In a few places, constant expressions are required by language rules (e.g., 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). 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).
It seems that Stroustrup is suggesting here that constexpr
ensures immutability of an object better than a traditional const
declaration. 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
?
He states in the beginning of the section:
C++ supports two notions of immutability
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.
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
:
is used primarily to specify interfaces
whereas 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.
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:
any attempt to modify a const object during its lifetime (3.8) results in undefined behavior
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.
Note that a constexpr variable is also const, from the draft C++11 standard section 7.1.5
The constexpr specifier:
A constexpr specifier used in an object declaration declares the object as const.
这篇关于是一个constexpr更“恒定”比const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!