Is a constexpr more “constant” than const?

梦想的初衷 提交于 2019-12-05 00:41:09

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.

const dosn't ensure bitwise constness, e.g. 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.

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. See also this discussion.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!