C++C++11C++14C++17C++20Learn C++Syntax

What is aligned_union in Modern C++

What is aligned union in Modern C++

The C++11 standard introduced alignment support as one of the many features of the C++ programming language that can be used with the latest C++ compilers today. One of the new features of this support was a new keyword align std::aligned_union that is used to provide a nested type which can be used as an uninitialized storage for any object whose size is at most the object size stated by the alignment size requirement. In this post, we explain how we can use align_union in Modern C++.

What is alignment support in modern C++?

When we talk about ‘alignment’ in C++ it means a set of hints or instructions that tell the compiler to place the physical representation of data structures and variables in memory so that they line up at specific intervals of bytes – the underlaying digital representation of all data items. Alignment tells the compiler and linker to add additional ‘space’ to a value in memory so that the next object begins ‘nicely’ on a particular memory boundary.

Due to the way the CPU and other logic chips in a computer work, alignment can help create more computationally efficient programs. Think of it in the way you cut a cake – if you use an even number of slices for your cake it’s much easier and quicker to divide it up because you can cut the cake into nice even chunks of two. If you get an odd number of cake slices, it’s a lot harder to work out how big those slices need to be to make sure you don’t have a huge slice of cake left over – or someone gets a tiny slice (or none) and stays hungry! By cutting the cake in half each time you are ‘aligning’ your cake slices evenly and optimally so all cake is efficiently used with no wasted cake.

The C++11 standard intends to extend the standard language and library with alignment-related features, known as alignment support. These alignment features include:

  • The alignment specifier alignas for declarations.
  • The alignof expression to retrieve alignment requirements of a type.
  • Alignment arithmetic by library support (aligned_storage, aligned_union).
  • std::align standard function for pointer alignment at run time.

Alignment support in C++ can be found in more detail here in the C++ standards [Note: PDF link]. We also discuss the alignof keyword in this blog post: https://learncplusplus.org/what-is-the-alignof-expression-in-modern-c/.

In this post, we will explain the aligned_union statement.

What does aligned_union mean in modern C++?

The aligned_union (std::aligned_union) is used to provide a nested type which can be used as an uninitialized storage for an object of any types listed in types. In other words, std::aligned_union provides a POD type large enough and suitably aligned to store a union type by the required align size. Here are the syntax and helper type for the align_union:

Syntax (Since C++11, deprecated in C++23):

Helper type (Since C++14, deprecated in C++23):

Note that aligned_union and aligned_storage are deprecated in C++23. There is also aligned_alloc which was introduced with the C++17 standards.

Is there a simple example of how to use aligned_union in modern C++?

Here are some examples how you can use aligned_union in C++.

and here is an example how you can use with a union:

and here is the aligned_union_t helper in use:

Is there a full example of how to use aligned_union in modern C++?

Here is a full C++ example of how to use aligned_union:

If you need more in-depth details about this feature, see the original Alignment Support Proposal document.

What is aligned union in Modern C++ The 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.

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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?