C++C++11C++14C++17Introduction to C++Learn C++Syntax

What Is The Unrestricted Unions Feature In Modern C++?

What Is The Unrestricted Unions Feature In Modern C++

Unions are rarely used but are another important data type alternative to structs and classes in modern C++ development. Unions are used to define variables that share storage space. The C++11 standard loosens up the restriction regarding members of unions, and in this post, we explain the unrestricted unions feature that came with C++11.

What is a union in modern C++?

Unions are used to define variables that share storage space. It is a special data type that can store only one of its non-static data members at a time. The compiler allocates enough storage in a number to accommodate the largest element in the union. Unlike a struct, the members of a union occupy the same location in memory. Writing into one overwrites all others.

Since C++11, here is the general syntax for the union:

We can use the record selector . to access elements of a union as in structs and classes.

How to use a union declaration in modern C++?

The general declaration syntax for unions is similar to that for structures. The differences are:

  • Unions can contain bit fields, but only one can be active. They all start at the beginning of the union. (Note that, because bit fields are machine dependent, they can pose problems when writing portable code.)
  • Unlike C++ structures, C++ union types cannot use the class access specifiers: publicprivate, and protected. All fields of a union are public.
  • Unions can be initialized only through their first declared member:

Here is a simple union declaration and definition,

A union can’t participate in a class hierarchy. It can’t be derived from any class, nor can it be a base class. A union can have a constructor.

What is the unrestricted unions feature in modern C++?

Starting with C++11, some of the restrictions mentioned above have been disabled. Since C++11, unions are able to contain objects that have a non-trivial constructor. If a union contains such an object, the implicit default constructor of the union is deleted, forcing the user to manually define a constructor.

Here is a simple example, let’s assume we have a special struct to store x, y, z coordinates:

C++11 allows us to use these kinds of classes and and their objects. Here is an example to unrestricted unions that uses the struct we define above,

Is there a full example of unrestricted unions in modern C++?

Here is the full example of how to use unrestricted unions in C++.

For more information on this feature, check this link and see Unrestricted unions Proposal document.

What is The Unrestricted Unions feature in modern C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++