且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

常数值变更

更新时间:2023-01-13 16:31:31

[dcl.type.cv] p4:

[dcl.type.cv]p4:


除了任何声明为 mutable ([dcl.stc])的类成员可以修改
,尝试在
生存期内修改([expr.ass],[expr.post.incr],
[expr.pre.incr])const对象([basic.type.qualifier])( [basic.life])导致行为不确定。

Except that any class member declared mutable ([dcl.stc]) can be modified, any attempt to modify ([expr.ass], [expr.post.incr], [expr.pre.incr]) a const object ([basic.type.qualifier]) during its lifetime ([basic.life]) results in undefined behavior.

[basic.type.qualifier] p1:

[basic.type.qualifier]p1:


常量对象是类型为 const T 的对象或非可变对象

A const object is an object of type const T or a non-mutable subobject of such an object.

cz 是一个const对象,因为它是 c 的不可变量子对象。您的代码尝试在其生命周期内对其进行修改。因此,该代码具有未定义的行为。

c.z is a const object, because it is a non-mutable subobject of c. Your code attempts to modify it during its lifetime. It follows that the code has undefined behavior.