File tree Expand file tree Collapse file tree 6 files changed +220
-0
lines changed
Expand file tree Collapse file tree 6 files changed +220
-0
lines changed Original file line number Diff line number Diff line change 1+ // d2mcpp: https://github.com/mcpp-community/d2mcpp
2+ // license: Apache-2.0
3+ // file: dslings/cpp11/16-generalized-unions-0.cpp
4+ //
5+ // Exercise/练习: cpp11 | 16 - generalized unions | 广义非平凡联合体
6+ //
7+ // Tips/提示:
8+ // - 允许初始化一个成员变量
9+ //
10+ // Docs/文档:
11+ // - https://cppreference.com/w/cpp/language/union.html
12+ // - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/16-generalized-unions.md
13+ //
14+ // 练习交流讨论: http://forum.d2learn.org/category/20
15+ //
16+ // Auto-Checker/自动检测命令:
17+ //
18+ // d2x checker generalized-unions
19+ //
20+
21+ #include < d2x/cpp/common.hpp>
22+
23+
24+ union M
25+ {
26+ int a1 = D2X_YOUR_ANSWER;
27+ int a2 = 21 ; // 移除多余的初始化
28+ D2X_YOUR_ANSWER a3;
29+ char c;
30+ };
31+
32+
33+ int main () {
34+
35+ M u1;
36+ // 初始化成员
37+ d2x_assert (u1.a1 == 42 );
38+ u1.a2 = 21 ;
39+
40+ double val = 3.14 ;
41+ u1.a3 = val;
42+
43+ u1.c = ' x' ;
44+
45+ D2X_WAIT
46+
47+ return 0 ;
48+ }
Original file line number Diff line number Diff line change 1+ // d2mcpp: https://github.com/mcpp-community/d2mcpp
2+ // license: Apache-2.0
3+ // file: dslings/cpp11/16-generalized-unions-1.cpp
4+ //
5+ // Exercise/练习: cpp11 | 16 - generalized unions | 广义非平凡联合体
6+ //
7+ // Tips/提示:
8+ // - 允许联合体包含非平凡类型(非 POD)
9+ //
10+ // Docs/文档:
11+ // - https://cppreference.com/w/cpp/language/union.html
12+ // - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/16-generalized-unions.md
13+ //
14+ // 练习交流讨论: http://forum.d2learn.org/category/20
15+ //
16+ // Auto-Checker/自动检测命令:
17+ //
18+ // d2x checker generalized-unions
19+ //
20+
21+ #include < d2x/cpp/common.hpp>
22+ #include < vector>
23+
24+ union M
25+ {
26+ int a1;
27+ D2X_YOUR_ANSWER a2;
28+ M () {}
29+ M (const std::vector<int >& vec) : a2 (vec) {
30+ }
31+ ~M () {}
32+ };
33+
34+
35+ int main () {
36+
37+ M u1;
38+ u1.a1 = 21 ;
39+
40+ // 1. 使用placement new构造
41+ new (&u1.a2 ) std::vector<int >();
42+
43+ u1.a2 = {1 , D2X_YOUR_ANSWER, 3 };
44+
45+ // 2. 验证:正常访问
46+ d2x_assert_eq (u1.a2 [1 ], 42 );
47+
48+ // 3. 手动析构
49+ u1.a2 .~vector ();
50+
51+ D2X_WAIT
52+
53+ return 0 ;
54+ }
Original file line number Diff line number Diff line change @@ -169,6 +169,14 @@ target("cpp11-15-variadic-templates-0")
169169target (" cpp11-15-variadic-templates-1" )
170170 add_files (" 15-variadic-templates-1.cpp" )
171171
172+ -- target: cpp11-16-generalized-unions
173+
174+ target (" cpp11-16-generalized-unions-0" )
175+ add_files (" 16-generalized-unions-0.cpp" )
176+
177+ target (" cpp11-16-generalized-unions-1" )
178+ add_files (" 16-generalized-unions-1.cpp" )
179+
172180-- target: cpp11-17-pod-type
173181
174182target (" cpp11-17-pod-type-0" )
Original file line number Diff line number Diff line change 1+ // d2mcpp: https://github.com/mcpp-community/d2mcpp
2+ // license: Apache-2.0
3+ // file: dslings/en/cpp11/16-generalized-unions-0.cpp
4+ //
5+ // Exercise: cpp11 | 16 - generalized unions | generalized (non-trivial) unions
6+ //
7+ // Tips:
8+ // - Allowing the initialization of a member variable
9+ //
10+ // Docs:
11+ // - https://cppreference.com/w/cpp/language/union.html
12+ // - https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp11/16-generalized-unions.md
13+ //
14+ // Discussion Forum: http://forum.d2learn.org/category/20
15+ //
16+ // Auto-Checker:
17+ //
18+ // d2x checker generalized-unions
19+ //
20+
21+ #include < d2x/cpp/common.hpp>
22+
23+
24+ union M
25+ {
26+ int a1 = D2X_YOUR_ANSWER;
27+ int a2 = 21 ; // Remove redundant initializations
28+ D2X_YOUR_ANSWER a3;
29+ char c;
30+ };
31+
32+
33+ int main () {
34+
35+ M u1;
36+ // Initialize union members
37+ d2x_assert (u1.a1 == 42 );
38+ u1.a2 = 21 ;
39+
40+ double val = 3.14 ;
41+ u1.a3 = val;
42+
43+ u1.c = ' x' ;
44+
45+ D2X_WAIT
46+
47+ return 0 ;
48+ }
Original file line number Diff line number Diff line change 1+ // d2mcpp: https://github.com/mcpp-community/d2mcpp
2+ // license: Apache-2.0
3+ // file: dslings/en/cpp11/16-generalized-unions-1.cpp
4+ //
5+ // Exercise: cpp11 | 16 - generalized unions | generalized (non-trivial) unions
6+ //
7+ // Tips:
8+ // - Allow unions to contain non-trivial types(non-POD)
9+ //
10+ // Docs:
11+ // - https://cppreference.com/w/cpp/language/union.html
12+ // - https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp11/16-generalized-unions.md
13+ //
14+ // Discussion Forum: http://forum.d2learn.org/category/20
15+ //
16+ // Auto-Checker:
17+ //
18+ // d2x checker generalized-unions
19+ //
20+
21+ #include < d2x/cpp/common.hpp>
22+ #include < vector>
23+
24+ union M
25+ {
26+ int a1;
27+ D2X_YOUR_ANSWER a2;
28+ M () {}
29+ M (const std::vector<int >& vec) : a2 (vec) {
30+ }
31+ ~M () {}
32+ };
33+
34+
35+ int main () {
36+
37+ M u1;
38+ u1.a1 = 21 ;
39+
40+ // 1. Construct with placement new
41+ new (&u1.a2 ) std::vector<int >();
42+
43+ u1.a2 = {1 , D2X_YOUR_ANSWER, 3 };
44+
45+ // 2. use assert to verify the contents
46+ d2x_assert_eq (u1.a2 [1 ], 42 );
47+
48+ // 3. Manually call the destructor
49+ u1.a2 .~vector ();
50+
51+ D2X_WAIT
52+
53+ return 0 ;
54+ }
Original file line number Diff line number Diff line change @@ -160,6 +160,14 @@ target("cpp11-14-type-alias-2")
160160target (" cpp11-14-type-alias-3" )
161161 add_files (" 14-type-alias-3.cpp" )
162162
163+ -- target: cpp11-16-generalized-unions
164+
165+ target (" cpp11-16-generalized-unions-0" )
166+ add_files (" 16-generalized-unions-0.cpp" )
167+
168+ target (" cpp11-16-generalized-unions-1" )
169+ add_files (" 16-generalized-unions-1.cpp" )
170+
163171-- target: cpp11-17-pod-type
164172
165173target (" cpp11-17-pod-type-0" )
You can’t perform that action at this time.
0 commit comments