Example of C++11 using union

  • 2020-06-07 04:59:26
  • OfStack

preface

union stands for union, which is a special class. As defined by the keyword union, an union can have multiple data members. For example,


union Token{
char cval;
int ival;
double dval;
};

In c++11, union behaves more and more like a class in addition to inheriting data Shared memory from the c language, such that members are of type public by default.

After C++11, many basic grammars have been modified. Among them, union's behavior develops towards class objects and expands on the basis of compatibility with the original syntax definition:

union can have member functions (including constructors and destructors), but not virtual functions union cannot participate in inheritance, cannot be a base class, and cannot be a subclass The member object of union cannot be a reference type

For union classes where all members are ES22en-ES23en, you can also use the same as before:


#include <iostream>
#include <cstdint>
union S
{
 std::int32_t n;  //  Take up 4 byte 
 std::uint16_t s[2]; //  Take up 4 byte 
 std::uint8_t c;  //  Take up 1 byte 
};      //  The overall footprint 4 byte 
 
int main()
{
 S s = {0x12345678}; //  Initialize the first 1 Three members, current s.n As active member 
 //  At this point, read  s.s  or  s.c  Undefined behavior 
 std::cout << std::hex << "s.n = " << s.n << '\n';
 s.s[0] = 0x0011; // s.s  Now active member 
 //  At this point, read  s.n  or  s.c  Undefined behavior 
 std::cout << "s.c is now " << +s.c << '\n' // 11 or 00,  Platform-dependent implementation 
    << "s.n is now " << s.n << '\n'; // 12340011 or 00115678
}

For all union classes containing non-ES28en-ES29en, then:

If non-static member (non - static) with a nontrivial special (non - trivial special) member functions (custom: copy/move constructors, copy/move assignment function, and the destructor), then the union class's default copy/move constructors and copy/move assignment function, the destructor will be deleted, if necessary, require the user to define your own implementation, if the user is not defined, is not to copy/move operation If a non-static (ES38en-ES39en) member has a non-trivial special (ES40en-ES41en special) constructor (a custom constructor), the default constructor for the union class is removed, requiring the user to define the implementation if necessary, and the union class cannot be instantiated if the user does not define it. Up to 1 variant member can have the default member initialization value You can't have static member data (which is weird because on clang you would report a link error instead of a syntax error), but you can have static member functions Access control for all members is public

Points 1 and 2 mean that if the member data type is nontrivial (ES48en-ES49en), then the union class needs to define the associated constructor, copy function, move construct, move assignment, destructor, and so on.


union A {
 int a;
 double b;
 std::string c;
 A() : c("111") {} //  because std::string Having non-trivial data types, 
 ~A() {}   //  the A You must customize the constructor and destructor, or you cannot instantiate them 
     //  Custom replication is also required if you want to implement replication semantics ( structure ) function 
};

The third point means:


union A {
 int a;
 double b;
 std::string c = "abc"; //  only 1 Six member data can have this initialization value 
 ~A(){};
};

conclusion


Related articles: