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
14
votes
2 answers

Cython: Nesting a union within a struct

In Cython glue declarations, how do I represent a C struct type containing an anonymous union? For example, if I have a C header file mystruct.h containing struct mystruct { union { double da; uint64_t ia; }; }; then, in…
polerto
  • 1,750
  • 5
  • 29
  • 50
13
votes
3 answers

What is the equivalent of boost::variant in the C++ standard library?

I am looking for an alternative to C-style union. boost::variant is one such option. Is there anything in std C++ ? union { int i; double d; }
cached
  • 569
  • 1
  • 7
  • 14
13
votes
8 answers

C++ union array and vars?

There's no way to do something like this, in C++ is there? union { { Scalar x, y; } Scalar v[2]; }; Where x == v[0] and y == v[1]?
mpen
  • 272,448
  • 266
  • 850
  • 1,236
13
votes
2 answers

Will the alignment of variables in a C union be the same on all systems?

Consider the following union: typedef union { struct { // Anonymous struct int red; int green; int blue; }; int colorChannels[3]; } Color; Regardless of the system this code is compiled or executed on, will the…
EKW
  • 2,059
  • 14
  • 24
13
votes
1 answer

What makes a union member active?

What makes a union member active? I've read chapter 9.5 of the C++14 standard (the one about unions), but I haven't found a clear answer to what makes a union member active. There is a note: In general, one must use explicit destructor calls and…
geza
  • 28,403
  • 6
  • 61
  • 135
13
votes
2 answers

constexpr reference to a variable of an anonymous struct

Given struct B, which inherits an anonymous union data member from struct A: #include struct A { union { struct { int a, b, c; }; int vals[3]; }; }; struct B: A { constexpr B(int x) : A{{{ x,…
plasmacel
  • 8,183
  • 7
  • 53
  • 101
13
votes
4 answers

Statically initialize anonymous union in C++

I am trying to statically initialize the following structure in Visual Studio 2010: struct Data { int x; union { const Data* data; struct {int x; int y; }; }; }; The following is fails with error C2440: 'initializing' :…
wpfwannabe
  • 14,587
  • 16
  • 78
  • 129
13
votes
4 answers

Why does my union not show the correct values?

union { int i; bool b; } x; x.i = 20000; x.b = true; cout << x.i; It prints out 19969. Why does it not print out 20000?
Joe
  • 133
  • 3
13
votes
1 answer

What is the point of 'protected' in a union in C++

Is there anything that protected members or functions can be used for? You cannot inherit from a union so there are no children that can access it. Does it provide a functional use or is just there because removing it was hassle?
Stefan
  • 3,669
  • 2
  • 32
  • 43
13
votes
3 answers

Questions regarding C++ non-POD unions

C++11 gave us to possibility to use non-POD types within unions, say I have the following piece of code; union { T one; V two; } uny; Somewhere within my class, only one member will be active at a time, now my questions are rather…
Skeen
  • 4,614
  • 5
  • 41
  • 67
13
votes
1 answer

How to Explain this C Union Output

#include union p { int x; char y; } k = {.y = 97}; int main() { printf("%d\n", k.y); return 0; } OUTPUT: 97 I came across this Question. As we know we can only initialize the first member of Union. But in this, at the…
user2485865
13
votes
1 answer

C++ anonymous structs

I use the following union to simplify byte, nibble and bit operations: union Byte { struct { unsigned int bit_0: 1; unsigned int bit_1: 1; unsigned int bit_2: 1; unsigned int bit_3: 1; unsigned int bit_4: 1; unsigned int…
Dejwi
  • 4,393
  • 12
  • 45
  • 74
13
votes
1 answer

Is accessing bytes of a __m128 variable via union legal?

Consider this variable declaration: union { struct { float x, y, z, padding; } components; __m128 sse; } _data; My idea is to assign the value through x, y, z fields, perform SSE2 computations and read the…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
13
votes
4 answers

How to access a struct member inside a union in C?

I have the following union: union employee { char key; struct manager { short int age; float shares; short int level; }; struct worker { short int age; short int skill; short…
amiregelz
  • 1,833
  • 7
  • 25
  • 46
12
votes
1 answer

Which union member becomes active after placement new

Regarding this code: #include int main() { union u { u() { i = 0; } ~u() {} int i; std::string s1; std::string s2; } u; new (&u) std::string{}; } [intro.object]/2 says that Objects…
Language Lawyer
  • 3,378
  • 1
  • 12
  • 29