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
6
votes
3 answers

generically convert from boost::variant to type

I have a typedef boost::variant variant which I use to store different types of values in a struct. Only one of the specific type is ever going to be stored in that struct,…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
6
votes
1 answer

Laravel 5 - How to use union in more than two tables/queries with Query Builder or Eloquent?

I have 10 tables that i want 'union'. Here my table name with same fields. sell_2007 sell_2008 sell_2009 ... sell_2015 sell_2016 In the example given by laravel do union in the two tables only (https://laravel.com/docs/5.3/queries#unions), how if…
Fredy
  • 2,840
  • 6
  • 29
  • 40
6
votes
4 answers

Is reading a member that wasn't the most recently written in GCC undefined behavior?

The C++ reference has the following explanation for unions, with the interesting part for this question in bold: The union is only as big as necessary to hold its largest data member. The other data members are allocated in the same bytes as part…
space_voyager
  • 1,984
  • 3
  • 20
  • 31
6
votes
2 answers

Proper syntax for using union with bit fields inside a struct

I have the following series of structs. struct FooWord1 { unsigned int Fill : 8; unsigned int someData1 : 18; unsigned int someData2 : 6; }; struct FooWord2 { unsigned int Fill : 8; union { unsigned int…
livefree75
  • 720
  • 8
  • 18
6
votes
2 answers

Is it legal to access a field of a returned union without a variable?

I have a function returning a union type. Is it allowed by the standard (C99) to access a field of a returned value directly from the call without copying the value to a variable. Here an example to illustrate what I mean: union thinga { int integ;…
Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48
6
votes
10 answers

ANSI C unions - are they really useful?

From a response to some question yesterday, I learned that it is nonportable and unsafe to write into one union member and read the value from another member of a different type, assuming underlying alignment of the members. So after some research I…
ysap
  • 7,723
  • 7
  • 59
  • 122
6
votes
11 answers

Advantages of using union when same thing can be done using struct - C

I have difficulty in understanding the use of union in C. I have read lot of posts here on SO about the subject. But none of them explains about why union is preferred when same thing can be achieved using a struct. Quoting from K&R As an example…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
6
votes
4 answers

C++ "Choice" Union

Not sure if there is a term for this, "choice" seems to work. I'm working in C++, and I have a bunch of unions I need to create where the union represents a choice of one of the members of the union. The current "choice" is tracked and is always…
LorenVS
  • 12,597
  • 10
  • 47
  • 54
6
votes
1 answer

Why is this not a constexpr?

#include union gc_bits { size_t value; struct { size_t arena : 2; } bits; constexpr gc_bits(size_t value_) : value(value_) { } }; static constexpr size_t get_max_arenas() { return…
DrPizza
  • 17,882
  • 7
  • 41
  • 53
6
votes
2 answers

How to expose C++ unions to Lua

For a project I'm working on, I need to expose some C++ classes in another library to Lua. Unfortunately, one of the most important classes in this library has lots of Unions and Enums (the sf::Event class from SFML), and from a quick Google search…
Ben Hollier
  • 585
  • 2
  • 11
  • 32
6
votes
2 answers

Active member of an union, uniform initialization and constructors

As the (Working Draft of) C++ Standard says: 9.5.1 [class.union] In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
6
votes
6 answers

C++ Union, Struct, Member type

If I have a class: class Odp { int i; int b; union { long f; struct { WCHAR* pwszFoo; HRESULT hr; }; }; } Union means that, of all values listed, it can only…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
6
votes
2 answers

C++ (Somehow) limit struct to parent union size

I'm attempting to create a color class of variable size- given a template-determined array of values, I'd like to create named aliases of each value in the array, ie: template class Color { public: union { …
Precursor
  • 622
  • 7
  • 18
6
votes
1 answer

hive querying records for a specific uniontype

I have a sample hive table created as CREATE TABLE union_test(foo UNIONTYPE, struct>); The data can be viewed as SELECT foo FROM union_test; The output…
Vikas Saxena
  • 1,073
  • 1
  • 12
  • 21
6
votes
2 answers

Union containing volatile structs

This seems to be similar to POD structs containing constant member, but sort of reversed. #include struct A { int a; }; union U { volatile A a; long b; }; int main() { U u1; U u2; u1.a.a = 12; u2 = u1; …
qbt937
  • 947
  • 7
  • 26