C++C++11C++14C++17Learn C++

What Is The alignas Alignment Specifier In Modern C++?

What Is The alignas Alignment Specifier In Modern C++

One of the features of Modern C++ is Alignment Support which comes with the C++11 standard. One of the new features of this support is a new keyword alignas which is the alignment specifier. For example, we can apply the alignas alignment specifier to define the minimum block size of data types such as the size of structs. In this post, we explain how we can use alignas in Modern C++ .

What is alignment support in modern C++?

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

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

In this post, we will explain the alignas alignment specifier.

What is the alignment specifier (alignas) in modern C++?

The alignas alignment specifier specifies the alignment requirement of a type or an object.

The syntax of alignas is:

Note that, this alignas specifier can be used with:

  • A declaration or definition of a class.
  • A declaration of a non-bitfield class data member.
  • A declaration of a variable (cannot be applied to a function parameter and cannot be the exception parameter of a catch clause).

Is there an alignas alignment specifier example in modern C++?

Here is a simple alignas alignment specifier example used in a struct.

this means, “align this in multiples of the size of the float type” which is 4 bytes. In other words, whatever you define the size of this struct will be in multiples of float type. That means it will be 4 bytes. If you add more variables or arrays inside, the size of this struct will be in multiples of 4. In other words, 8,12, 16, 64, 128 bytes and so on all word as does 24 bytes, 32bytes etc.

Instead of a float type, you can directly define its size of it with its size. The example below is equivalent to the previous example:

Is there a full example of how to use the alignas alignment specifier in modern C++?

This full example explains with two different struct examples.

Here the struct st1 has 2 bytes (x and y) size but its size will be 4 because of alignas(4) specifier. In the second struct st2, we added z[4] bytes. So in total, this st2 struct has 6 bytes size, but again because of alignas(4) it’s size will be in multiples of 4, so the closest next size is 8, it will be 8 bytes.

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

What Is The alignas Alignment Specifier 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.

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++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?