Questions tagged [unions]

The plural of a keyword in the C family of languages for declaring a union data type.

A union is a single variable with multiple possible representations. For example:

union foo {
    int a;
    float b;
    struct {
        char c, d;
    } e;
};

is a variable foo that can be accessed as either an int, float, or struct. Although similar in format to a struct, the size of a union is equal to the size of the largest field where as the size of a structure is the sum of the sizes of all fields.

They can also be used for a primitive form of polymorphism. A program can check for types and use different fields in a union in different contexts.


For the SQL UNION keyword, use instead of this tag.

1638 questions
24
votes
2 answers

C++: unions with methods?

Is there anything wrong with a union having one or more methods? Or anything to watch out for? (I can see constructors/destructors being problematic for schizophrenic reasons)
Jason S
  • 184,598
  • 164
  • 608
  • 970
23
votes
3 answers

An union with a const and a nonconst member?

This appears to be undefined behavior union A { int const x; float y; }; A a = { 0 }; a.y = 1; The spec says Creating a new object at the storage location that a const object with static, thread, or automatic storage duration occupies or, at…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
22
votes
5 answers

Union in c++ are they feasible

Can a union in C++ have a member function? How do union with data members and member functions exist if an object is created? If I suppose yes, then are they feasible any where. If yes then where?
Vijay
  • 65,327
  • 90
  • 227
  • 319
22
votes
3 answers

C++ unions vs. reinterpret_cast

It appears from other StackOverflow questions and reading §9.5.1 of the ISO/IEC draft C++ standard standard that the use of unions to do a literal reinterpret_cast of data is undefined behavior. Consider the code below. The goal is to take the…
kgraney
  • 1,975
  • 16
  • 20
21
votes
12 answers

Union – useless anachronism or useful old school trick?

I recently came across a great data structures book,"Data Structures Using C" (c) 1991, at a local Library book sale for only $2. As the book's title implies, the book covers data structures using the C programming language. I got the book knowing…
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
21
votes
5 answers

How to initialize a union?

If it's a struct then it can be done *p = {var1, var2..}; But seems this doesn't work with union: union Ptrlist { Ptrlist *next; State *s; }; Ptrlist *l; l = allocate_space(); *l = {NULL}; Only to get: expected expression…
lexer
  • 1,027
  • 3
  • 14
  • 18
21
votes
1 answer

Static vector internal data layout - `union` vs `std::aligned_storage_t` - huge performance difference

Assume that you have to implement a static_vector class, which is a fixed capacity container that entirely lives on the stack and never allocates, and exposes an std::vector-like interface. (Boost provides boost::static_vector.) Considering…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
21
votes
6 answers

C union type in Swift?

How can I declare and use a C union type in Swift? I tried: var value: union { var output: CLongLong var input: [CInt] } but it does not work... UPDATED: I want to use union to split a 8 bytes number to 2 x 4 bytes number.
zendobk
  • 548
  • 1
  • 5
  • 17
20
votes
4 answers

strict aliasing and alignment

I need a safe way to alias between arbitrary POD types, conforming to ISO-C++11 explicitly considering 3.10/10 and 3.11 of n3242 or later. There are a lot of questions about strict aliasing here, most of them regarding C and not C++. I found a…
cooky451
  • 3,460
  • 1
  • 21
  • 39
20
votes
1 answer

Union common initial sequence with primitive

I am trying to better understand a rather surprising discovery regarding unions and the common initial sequence rule. The common initial sequence rule says (class.mem 23):  In a standard-layout union with an active member of struct type T1, it is…
Nir Friedman
  • 17,108
  • 2
  • 44
  • 72
20
votes
3 answers

Anonymous union and struct

How would you go about doing this in standard C++11/14 ? Because if I'm not mistaken this isn't standard compliant code with the anonymous structs. I wish to access the members the same way as you would with this. template
Grandstack
  • 323
  • 1
  • 2
  • 7
20
votes
4 answers

Using a union with unique_ptr

Trying to use a unique_ptr inside a union gives me a segfault when I try to std::move or std::make_unique it. #include #include union myUnion{ struct{std::unique_ptr upFloat;}structUpFloat; …
jacksawild
  • 255
  • 2
  • 6
20
votes
1 answer

Is a union in C++ actually a class?

A junior developer asked me if it was possible to overload assignment operators for a union with POD arguments such that the corresponding data type within the union would get written to when an instance of the union is assigned to variable of that…
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
19
votes
3 answers

What are Unrestricted Unions proposed in C++11?

I gather unrestricted unions as one of the functionality being put forth in C++11. Can anyone please explain the semantics behind this and the advantages it provides?
Alok Save
  • 202,538
  • 53
  • 430
  • 533
19
votes
4 answers

Usage of union inside a class

I saw some code as follows: class A { private: union { B *rep; A *next; }; // no variables of this anonymous defined! void func() { A *p = new A; p->next = NULL; // why p has a member variable of…
q0987
  • 34,938
  • 69
  • 242
  • 387