Questions tagged [standard-layout]
36 questions
590
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
- 120,726
- 52
- 304
- 418
46
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
- 29,265
- 19
- 105
- 197
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
- 130,300
- 18
- 171
- 314
28
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
- 35,107
- 16
- 95
- 241
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
- 13,729
- 3
- 40
- 61
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
- 91,815
- 8
- 136
- 249
21
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
- 247,587
- 26
- 487
- 819
18
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
- 1,996
- 2
- 23
- 37
13
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
- 26,117
- 6
- 47
- 111
13
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
- 35,820
- 10
- 96
- 156
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
- 48,511
- 12
- 121
- 172
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
- 82,972
- 25
- 221
- 369
10
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
- 4,937
- 2
- 26
- 41
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
- 33,741
- 27
- 150
- 304
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
- 445
- 4
- 9