#include <new>
#include <string>
constexpr int TU_STRING = 0;
constexpr int TU_INT = 1;
constexpr int TU_FLOAT = 2;
struct TU {
union my_union {
struct i_type {
int type;
int i;
} i;
struct f_type {
int type;
float f;
} f;
struct s_type {
int type;
std::string s;
} s;
my_union(int i) : i{TU_INT, i} {}
my_union(float f) : f{TU_FLOAT, f} {}
my_union(std::string s) : s{TU_STRING, std::move(s)} {}
my_union(my_union const &other) {
// This is safe.
switch (other.i.type) {
case TU_INT:
::new (&i) auto(other.i);
break;
case TU_FLOAT:
::new (&f) auto(other.f);
break;
case TU_STRING:
::new (&s) auto(other.s);
break;
}
}
~my_union() {
// This is safe.
if (TU_STRING == s.type) {
s.~s_type();
}
}
} u;
TU(int i) : u(i) {}
TU(float f) : u(f) {}
TU(std::string s) : u(std::move(s)) {}
TU(TU const &) = default;
~TU() = default;
};
Showing posts with label cpp_union. Show all posts
Showing posts with label cpp_union. Show all posts
Jul 18, 2022
[C++][note] union tagging sample code
May 11, 2018
[C++][C++17] sum type
std::variant sum types
std::visit process the values in a std::variant
Sum types are compound types that have a range of values that is the sum of the ranges of their parts.
std::variant as a "safer union"
std::visit process the values in a std::variant
Sum types are compound types that have a range of values that is the sum of the ranges of their parts.
std::variant as a "safer union"
using ItsOk = std::variant<std::vector<int>, double>;
int main() {
ItsOk io = std::vector{22, 44, 66}; //set the variant to vector, this constructs the internal vector
io = 13.7; // reset to double - the internal vector is properly destroyed
int i = std::get<std::vector<int>>(io)[2]; // There's no vector in the variant - throws an exception
}
std::ostream& operator<< (std::ostream& os, MyVariant const& v) {
std::visit([&os](auto const& e){ os << e; }, v);
return os;
}
Mar 19, 2018
[C++] Union/StandardLayoutType can not have reference data member.
http://en.cppreference.com/w/cpp/concept/StandardLayoutType
Requirements
About union's data member:
Code from cppref:
Requirements
- All non-static data members have the same access control
- Has no virtual functions or virtual base classes
- Has no non-static data members of reference type
- All non-static data members and base classes are themselves standard layout types
- A union cannot have data members of reference types.
Why?
It is illegal for a union to contain a reference member. This is presumably because references are not objects and it's unspecified whether or not they occupy storage
---so it makes little sense for a reference to share its storage with other variables.
- A union can have member functions (including constructors and destructors), but not virtual (10.3) functions.
- A union shall not have base classes.
- A union shall not be used as a base class.
- If a union contains a non- static data member of reference type the program is ill-formed.
About union's data member:
- If a union contains a non-static data member with a non-trivial special member function (copy/moveconstructor, copy/move assignment, or destructor), that function is deleted by default in the union and needs to be defined explicitly by the programmer.
- If a union contains a non-static data member with a non-trivial default constructor, the default constructor of the union is deleted by default unless a variant member of the union has a default member initializer .
- At most one variant member can have a default member initializer.
#include <iostream>
#include <string>
#include <vector>
union S
{
std::string str;
std::vector<int> vec;
~S() {} // needs to know which member is active, only possible in union-like class
}; // the whole union occupies max(sizeof(string), sizeof(vector<int>))
int main()
{
S s = {"Hello, world"};
// at this point, reading from s.vec is undefined behavior
std::cout << "s.str = " << s.str << '\n';
s.str.~basic_string<char>();
new (&s.vec) std::vector<int>;
// now, s.vec is the active member of the union
s.vec.push_back(10);
std::cout << s.vec.size() << '\n';
s.vec.~vector<int>();
}
Subscribe to:
Posts (Atom)