问题
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?
回答1:
[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.
回答2:
The foo
-function itself would be OK, as const
member functions like T::foo() const
just indicate that this
is of type const *T
; The fact that a (non-const) member points to the same object is irrelevant then.
The object c
in first place, however, is declared as const. So it is UB to alter the contents of c
through whatever code, including the (per se correct) member function foo
.
来源:https://stackoverflow.com/questions/52710231/constant-value-changing