Constant value changing

  • A+
Category:Languages

Suppose I have a struct definition:

struct thing {     thing* x;     int z;      thing() : x(this), z(0) {}     void foo() const     {         this->x->z++;     } }; 

Note that I create a mutable pointer to myself (evil laugh)

And then I can use this later like this:

int main() {     const thing c;     c.foo();     assert(c.z == 1);     c.foo();     assert(c.z == 2);     return c.z; } 

And as you can see it seems that I can change a constant value......is this UB?

 


[dcl.type.cv]p4:

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:

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

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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: