Skip to content

Commit bfa699d

Browse files
committed
pod2
1 parent 856c318 commit bfa699d

1 file changed

Lines changed: 44 additions & 13 deletions

File tree

‎cpp/pod.cpp‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
#include <type_traits>
12+
#include <vector>
1213

1314
int main() {
1415
/* POD restrictions have become more and more relaxed as the standard evolved.
@@ -22,16 +23,36 @@ int main() {
2223
* https://en.cppreference.com/w/cpp/named_req/TrivialType
2324
*/
2425
{
25-
/* Not trivial because we removed the default constructor
26-
* by using our own custom non-default constructor.
26+
/* Has one or more default constructors, all of which are either
27+
* trivial or deleted, and at least one of which is not deleted.
2728
*/
2829
{
29-
struct C {
30-
C(int i) {}
31-
};
32-
static_assert(std::is_trivially_copyable<C>());
33-
static_assert(!std::is_trivial<C>());
34-
static_assert(!std::is_pod<C>());
30+
/* Not trivial because we removed the default constructor
31+
* by using our own custom non-default constructor.
32+
*/
33+
{
34+
struct C {
35+
C(int i) {}
36+
};
37+
static_assert(std::is_trivially_copyable<C>());
38+
static_assert(!std::is_trivial<C>());
39+
static_assert(!std::is_pod<C>());
40+
}
41+
42+
/* No, this is not a default trivial constructor either:
43+
* https://en.cppreference.com/w/cpp/language/default_constructor
44+
*
45+
* The constructor is not user-provided (i.e., is implicitly-defined or
46+
* defaulted on its first declaration)
47+
*/
48+
{
49+
struct C {
50+
C() {}
51+
};
52+
static_assert(std::is_trivially_copyable<C>());
53+
static_assert(!std::is_trivial<C>());
54+
static_assert(!std::is_pod<C>());
55+
}
3556
}
3657

3758
/* Not trivial because not trivially copyable. */
@@ -110,6 +131,17 @@ int main() {
110131
static_assert(std::is_standard_layout<C>());
111132
static_assert(std::is_pod<C>());
112133
}
134+
135+
/* Just one base class with non-static member: Base1, Base2 has none. */
136+
{
137+
struct Base1 {
138+
int i;
139+
};
140+
struct Base2 {};
141+
struct C : Base1, Base2 {};
142+
static_assert(std::is_standard_layout<C>());
143+
static_assert(std::is_pod<C>());
144+
}
113145
}
114146

115147
/* Base classes of the same type as the first non-static data member.
@@ -187,13 +219,12 @@ int main() {
187219
#endif
188220
}
189221

190-
/* Derived.
191-
* https://stackoverflow.com/questions/4762788/can-a-class-with-all-private-members-be-a-pod-class/4762944#4762944
222+
/* Standard library containers are not, for the most part (all?),
223+
* POD because they are not trivial.
192224
*/
193225
{
194-
struct C {};
195-
struct D : C {};
196-
static_assert(std::is_pod<D>());
226+
static_assert(!std::is_pod<std::vector<int>>());
227+
static_assert(!std::is_trivially_copyable<std::vector<int>>());
197228
}
198229
}
199230
#endif

0 commit comments

Comments
 (0)