C++ Logo

std-proposals

Advanced search

Re: [std-proposals] I have a span but I need a container

From: Simon Schröder <dr.simon.schroeder_at_[hidden]>
Date: Wed, 18 Jun 2025 06:57:25 +0200
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.

C++ adds on top of this object lifetimes which usually start and end with constructors and destructors. If you start the lifetime of a Pretender object you don’t start the lifetime of a std::string object (unless you inherit from it which IIRC is also prohibited by the standard (or at least restricted because the destructor is not virtual)).

Received on 2025-06-18 04:57:39