|
| 1 | +<div align=right> |
| 2 | + |
| 3 | + 🌎 [中文] | [English] |
| 4 | +</div> |
| 5 | + |
| 6 | +[中文]: ./16-generalized-unions.md |
| 7 | +[English]: ../en/cpp11/16-generalized-unions.md |
| 8 | + |
| 9 | +# Generalized Unions |
| 10 | + |
| 11 | +C++11 introduced `generalized (non-trivial) unions`. |
| 12 | + |
| 13 | +Union members share memory. |
| 14 | +The size of a union is at least large enough to hold the largest data member. |
| 15 | + |
| 16 | +| Book | Video | Code | X | |
| 17 | +| --- | --- | --- | --- | |
| 18 | +| [cppreference-union](https://cppreference.com/w/cpp/language/union.html) / [markdown](https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp11/16-generalized-unions.md) | [Video Explanation]() | [Exercise Code]() | | |
| 19 | + |
| 20 | +**Why introduced?** |
| 21 | + |
| 22 | +- Can directly hold objects like std::string, without needing pointers. |
| 23 | +- Better management of member lifetimes. |
| 24 | + |
| 25 | +**How Current Unions Differ from Before?** |
| 26 | + |
| 27 | +- At most one variant member can have a default member initializer. |
| 28 | +- Unions can contain non-static data members with non-trivial special member functions. |
| 29 | + |
| 30 | +```c++ |
| 31 | +union S { |
| 32 | + int a; |
| 33 | + float b; |
| 34 | + std::string str; // Before C++11, such members could not be placed directly, or static members were used. |
| 35 | + S() {} |
| 36 | + ~S() {} |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## I. Basic Usage and Scenarios |
| 41 | + |
| 42 | +**Usage of Ordinary Unions** |
| 43 | +> Only one value is valid at a time. |
| 44 | +```cpp |
| 45 | +union M { |
| 46 | + int a; |
| 47 | + double b; |
| 48 | + char *str; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +**Usage of Generalized Unions** |
| 53 | +> The size of the union is the maximum space occupied by its data members, which changes dynamically based on the active member. |
| 54 | +```cpp |
| 55 | +#include <iostream> |
| 56 | +#include <string> |
| 57 | +#include <vector> |
| 58 | + |
| 59 | +union M { |
| 60 | + int a; |
| 61 | + int b; |
| 62 | + std::string str; |
| 63 | + std::vector<int> arr; |
| 64 | + M(int a) : b(a) { } |
| 65 | + M(const std::string &s) : str(s) { } |
| 66 | + M(const std::vector<int> &a) : arr(a) { } |
| 67 | + ~M() { } // Needs to know which data member is active to destruct correctly. |
| 68 | +}; |
| 69 | + |
| 70 | +int main() { |
| 71 | + M m("123456"); |
| 72 | + std::cout << "m.str = " << m.str<< std::endl; |
| 73 | + m.arr = { 1, 2, 3, 4, 5, 6 }; |
| 74 | + std::cout << "m.arr = "; |
| 75 | + for(int v: m.arr) { |
| 76 | + std::cout << v << " "; |
| 77 | + } |
| 78 | + std::cout << std::endl; |
| 79 | + return 0; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +**Lifetime** |
| 84 | +> A member's lifetime begins when it becomes active and ends when it becomes inactive. |
| 85 | +
|
| 86 | +```cpp |
| 87 | +#include <iostream> |
| 88 | + |
| 89 | +struct Life { |
| 90 | + Life() { std::cout << "----Life(" << this << ") Start----" << std::endl; } |
| 91 | + ~Life() { std::cout << "----Life(" << this << ") End----" << std::endl; } |
| 92 | +}; |
| 93 | + |
| 94 | +union M { |
| 95 | + int a; |
| 96 | + Life l; |
| 97 | + M(int n) : a(n) { } |
| 98 | + M(const Life &life) : l(life) { } |
| 99 | + ~M() { } // Needs to know which data member is active to destruct. |
| 100 | +}; |
| 101 | + |
| 102 | +int main() { |
| 103 | + M m = 1; |
| 104 | + std::cout << "Life 1 time one Start" << std::endl; |
| 105 | + m = Life(); |
| 106 | + // The Life object's lifetime ends before it becomes inactive here. |
| 107 | + std::cout << "Life 1 time one End" << std::endl; |
| 108 | + m = 2; |
| 109 | + std::cout << "Life 2 time one Start" << std::endl; |
| 110 | + m = Life(); |
| 111 | + std::cout << "Life 2 time one Start" << std::endl; |
| 112 | + m = 3; |
| 113 | + return 0; |
| 114 | +} |
| 115 | +``` |
| 116 | +
|
| 117 | +**Anonymous Unions** |
| 118 | +
|
| 119 | +```cpp |
| 120 | +int main() { |
| 121 | + union { |
| 122 | + int a; |
| 123 | + const char *b; |
| 124 | + }; |
| 125 | + a = 1; |
| 126 | + b = "Jerry"; |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## II. Precautions |
| 131 | + |
| 132 | +**Accessibility** |
| 133 | + |
| 134 | +Like `struct`, the default member access for a `union` is `public`. |
| 135 | + |
| 136 | +**Destruction of Unions** |
| 137 | +> Destructors for unions are generally not defined because the union itself cannot know which member is active. |
| 138 | + |
| 139 | +```cpp |
| 140 | +union M { |
| 141 | + char* str1; |
| 142 | + char* str2; |
| 143 | + ~M() { |
| 144 | + delete str1; // Error if the active member is str2. |
| 145 | + } |
| 146 | +}; |
| 147 | +``` |
| 148 | + |
| 149 | +**Limitations of Anonymous Unions** |
| 150 | +> Anonymous unions cannot contain member functions or static data members. |
| 151 | +
|
| 152 | +```cpp |
| 153 | +union { |
| 154 | + int a; |
| 155 | + static int b; // Error: cannot have static data members. |
| 156 | + int print() {...}; // Error: cannot have member functions. |
| 157 | +}; |
| 158 | +``` |
| 159 | + |
| 160 | +**Undefined Behavior** |
| 161 | +> Accessing an inactive member results in undefined behavior. |
| 162 | +
|
| 163 | +```cpp |
| 164 | +union M { |
| 165 | + int a; |
| 166 | + double b; |
| 167 | +}; |
| 168 | + |
| 169 | +M m; |
| 170 | +m.a = 1; |
| 171 | +double c = m.b; // Error: undefined behavior. |
| 172 | +``` |
| 173 | + |
| 174 | +## III. Exercise Code |
| 175 | + |
| 176 | +TODO |
| 177 | + |
| 178 | +## IV. Other |
| 179 | + |
| 180 | +- [Discussion Forum](https://forum.d2learn.org/category/20) |
| 181 | +- [d2mcpp Tutorial Repository](https://github.com/mcpp-community/d2mcpp) |
| 182 | +- [Tutorial Video List](https://space.bilibili.com/65858958/lists/5208246) |
| 183 | +- [Tutorial Support Tool - xlings](https://github.com/d2learn/xlings) |
0 commit comments