Date: Wed, 18 Jun 2025 01:49:36 -0400
On Wed, Jun 18, 2025 at 12:57 AM Simon Schröder via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> There is one thing that the standard guarantees which is why in most (or even all) implementations this trick currently works: If member variables are defined in the same order in two different structs they are layout compatible. This is something that C specifies right from the beginning and C++ has taken over. It is why including a header in multiple translation units actually works. So, there is a guarantee for the layout.
Note that this is only true for standard layout types... which
`std::string` is not.
No, the reason why this "works" is that implementations need to give
subobjects *some* particular layout within a higher level object.
Because that layout must be consistent across all uses of that type
within that implementation, if you know what an object's layout is for
your implementation, you can build something which mimics that layout.
But it only works for that implementation. And it requires knowing
very specific implementation details about an object which you are not
generally privy to.
<std-proposals_at_[hidden]> wrote:
>
> There is one thing that the standard guarantees which is why in most (or even all) implementations this trick currently works: If member variables are defined in the same order in two different structs they are layout compatible. This is something that C specifies right from the beginning and C++ has taken over. It is why including a header in multiple translation units actually works. So, there is a guarantee for the layout.
Note that this is only true for standard layout types... which
`std::string` is not.
No, the reason why this "works" is that implementations need to give
subobjects *some* particular layout within a higher level object.
Because that layout must be consistent across all uses of that type
within that implementation, if you know what an object's layout is for
your implementation, you can build something which mimics that layout.
But it only works for that implementation. And it requires knowing
very specific implementation details about an object which you are not
generally privy to.
Received on 2025-06-18 05:49:50