Date: Wed, 18 Jun 2025 10:21:11 +0100
On Wed, 18 Jun 2025 at 08:32, Robin Savonen Söderholm via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> No, it can't. You have the same guarantee of layout for string as well.
>
No you don't.
> The only time that you don't have those guarantees (afaik) are when you
> mix visibility of member variables (the guarantee holds per visibility)
>
That changed in C++23:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1847r4.pdf
> or when you are using virtual inheritance ( i.e like class foo : virtual
> bar {...}; ). Multi inheritance has some implementation-defined layout but
> the members between bases should also be ordered according to the order of
> inheritance.
>
The standard dictates that the subobjects must be arranged in memory in the
same order as they're declared, but doesn't guarantee more than the order.
In particular, the standard does not require non-trivially copyable
non-standard layout types to even occupy contiguous bytes in memory. There
can be gaps of unreadable or inaccessible bytes in the middle of an object:
"An object of trivially copyable or standard-layout type (6.8.1) shall
occupy contiguous bytes of storage."
std::string is neither trivially copyable nor standard-layout.
Even if the implementation doesn't actually place the subobjects in
non-contiguous regions of storage, for a non-standard-layout type the
compiler can place arbitrary padding between the members (as long as the
alignment for the members is still correct, obviously).
std-proposals_at_[hidden]> wrote:
> No, it can't. You have the same guarantee of layout for string as well.
>
No you don't.
> The only time that you don't have those guarantees (afaik) are when you
> mix visibility of member variables (the guarantee holds per visibility)
>
That changed in C++23:
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1847r4.pdf
> or when you are using virtual inheritance ( i.e like class foo : virtual
> bar {...}; ). Multi inheritance has some implementation-defined layout but
> the members between bases should also be ordered according to the order of
> inheritance.
>
The standard dictates that the subobjects must be arranged in memory in the
same order as they're declared, but doesn't guarantee more than the order.
In particular, the standard does not require non-trivially copyable
non-standard layout types to even occupy contiguous bytes in memory. There
can be gaps of unreadable or inaccessible bytes in the middle of an object:
"An object of trivially copyable or standard-layout type (6.8.1) shall
occupy contiguous bytes of storage."
std::string is neither trivially copyable nor standard-layout.
Even if the implementation doesn't actually place the subobjects in
non-contiguous regions of storage, for a non-standard-layout type the
compiler can place arbitrary padding between the members (as long as the
alignment for the members is still correct, obviously).
Received on 2025-06-18 09:21:28