-
Notifications
You must be signed in to change notification settings - Fork 160
/
aggregate.cpp
77 lines (66 loc) · 1.71 KB
/
aggregate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "common.hpp"
int main() {
// # Aggregate initialization
//
// Aggregate is a form of initializer list constructor.
//
// That constructor gets defined automatically.
//
// It is only available for aggregate types.
//
// Having such a constructor seems to be the main property of aggregates.
// https://en.cppreference.com/w/cpp/language/aggregate_initialization
{
struct C {
int i;
int j;
};
struct D {
int i;
int j;
C c;
};
#if __cplusplus >= 201703L
static_assert(std::is_aggregate<C>());
static_assert(std::is_aggregate<D>());
#endif
// Works like C struct.
{
C c{1, 2};
assert(c.i == 1);
assert(c.j == 2);
}
// Like C struct again.
{
C c{1};
assert(c.i == 1);
assert(c.j == 0);
}
// Also like C when there are sub-structs..
{
D d{1, 2, {3, 4}};
assert(d.i == 1);
assert(d.j == 2);
assert(d.c.i == 3);
assert(d.c.j == 4);
}
// With private members, it is not an aggregate type.
{
struct E {
private:
int i;
int j;
};
#if __cplusplus >= 201703L
static_assert(!std::is_aggregate<E>());
#endif
// ERROR. No such constructor.
//E e{1, 2};
// Being POD does not imply being an aggregate.
static_assert(std::is_pod<E>());
}
}
// TODO full list of everything that makes something not aggregate.
{
}
}