Questions tagged [standard-layout]
42 questions
684
votes
6 answers
What are Aggregates and PODs and how/why are they special?
This FAQ is about Aggregates and PODs and covers the following material:
What are Aggregates?
What are PODs (Plain Old Data)?
How are they related?
How and why are they special?
What changes for C++11?

Armen Tsirunyan
- 130,161
- 59
- 324
- 434
52
votes
6 answers
Why is C++11's POD "standard layout" definition the way it is?
I'm looking into the new, relaxed POD definition in C++11 (section 9.7)
A standard-layout class is a class that:
has no non-static data members of type non-standard-layout class (or array of such types) or reference,
has no virtual functions…

spraff
- 32,570
- 22
- 121
- 229
45
votes
1 answer
Does C++20 remove the requirement for class members to be in ascending order?
In C++17 there is normative text [class.mem]/17:
Non-static data members of a (non-union) class with the same access control (Clause 14) are allocated so
that later members have higher addresses within a class object. The order of allocation of…

M.M
- 138,810
- 21
- 208
- 365
37
votes
3 answers
Is a Union Member's Destructor Called
C++11 allowed the use of standard layout types in a union: Member of Union has User-Defined Constructor
My question then is: Am I guaranteed the custom destructor will be called, when the union goes out of scope?
My understanding is that we must…

Jonathan Mee
- 37,899
- 23
- 129
- 288
27
votes
2 answers
Guaranteed memory layout for standard layout struct with a single array member of primitive type
Consider the following simple struct:
struct A
{
float data[16];
};
My question is:
Assuming a platform where float is a 32-bit IEEE754 floating point number (if that matters at all), does the C++ standard guarantee the expected memory layout…

lisyarus
- 15,025
- 3
- 43
- 68
22
votes
2 answers
Is being a POD type exactly equivalent to being a trivial, standard-layout type?
In C++20, the concept of POD is deprecated, supposedly because it is a meaningless composite trait of being trivial and standard-layout. However, the definition of POD in the C++20 draft is not exactly "both trivial and standard-layout"; it is…

Brian Bi
- 111,498
- 10
- 176
- 312
22
votes
5 answers
Standard-layout and tail padding
David Hollman recently tweeted the following example (which I've slightly reduced):
struct FooBeforeBase {
double d;
bool b[4];
};
struct FooBefore : FooBeforeBase {
float value;
};
static_assert(sizeof(FooBefore) >…

Barry
- 286,269
- 29
- 621
- 977
19
votes
1 answer
Standard Layout c++
I was going through great articles on C++ POD, Trivial and Standard Layout classes
One property I haven't clearly understood about standard layout is the following:-
A standard layout has no base classes of the same type as the first
…

jmishra
- 2,086
- 2
- 24
- 38
14
votes
1 answer
Union of layout-compatible types
Look at this code:
struct A {
short s;
int i;
};
struct B {
short s;
int i;
};
union U {
A a;
B b;
};
int fn() {
U u;
u.a.i = 1;
return u.b.i;
}
Is it guaranteed that fn() returns 1?
Note: this is a follow-up…

geza
- 28,403
- 6
- 61
- 135
14
votes
3 answers
Common initial sequence and alignment
While thinking of a counter-example for this question, I came up with:
struct A
{
alignas(2) char byte;
};
But if that's legal and standard-layout, is it layout-compatible to this struct B?
struct B
{
char byte;
};
Furthermore, if we…

dyp
- 38,334
- 13
- 112
- 177
13
votes
1 answer
How is is_standard_layout useful?
From what I understand, standard layout allows three things:
Empty base class optimization
Backwards compatibility with C with certain pointer casts
Use of offsetof
Now, included in the library is the is_standard_layout predicate metafunction, but…

Pubby
- 51,882
- 13
- 139
- 180
12
votes
3 answers
Is it undefined behavior to read and compare padding bytes of a POD type?
Today I've encountered some code that roughly looks like the following snippet. Both valgrind and UndefinedBehaviorSanitizer detected reads of uninitialized data.
template
void foo(const T& x)
{
static_assert(std::is_pod_v &&…

Vittorio Romeo
- 90,666
- 33
- 258
- 416
11
votes
1 answer
Can I legally reinterpret_cast between layout-compatible standard-layout types?
I'm writing a class that, assuming the answer to Are enumeration types layout compatible with their underlying type? is "yes", is layout-compatible struct kevent but uses enum classes for filter, flags, etc. with the proper underlying types for the…

Shea Levy
- 5,237
- 3
- 31
- 42
9
votes
1 answer
reinterpret_cast vs. static_cast for writing bytes in standard-layout types?
I need to write to individual bytes of some integer types. Should I used reinterpret_cast, or should I use static_cast via void*?
(a)
unsigned short v16;
char* p = static_cast(static_cast(&v16));
p[1] = ... some char value
p[0] = ...…

Martin Ba
- 37,187
- 33
- 183
- 337
7
votes
1 answer
C++ Standard Layout and References
According to the C++ standard:
A standard-layout class is a class that:
—has no non-static data members of type non-standard-layout class (or array of such types) or reference.
What property(ies) of references prevent classes with reference…

TRISAbits
- 455
- 4
- 9