C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Alteration of elements in a set (non-const iterator)

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 22 May 2023 09:44:15 -0400
On Mon, May 22, 2023 at 6:53 AM Oleksandr Koval via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>>
>> That pair holds the "first" data member with type const T1. That's very much a const object.
>
> Hm, I thought that because it's inside a struct, it's actually a "subobject", not an "object". I always understood const_cast rule in a way that you can change const-ness as long as the underlying memory is not a read-only and I don't understand how it's possible to have a struct which has some members of it in a read-only memory and others in a read-write one.

As far as the standard is concerned, there is no such thing as
"read-only memory". There are objects which are declared `const`, and
objects which are not.

[dcl.type.cv]/4 says:

> 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.

And [basic.type.qualifier] says:

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

None of these require the object to be a "complete object" (the
standard terminology for an object that isn't a subobject of some
object). Subobjects are objects, so this rule applies to them too.

Received on 2023-05-22 13:44:27