Namespaces
Variants
Views
Actions

alignas specifier

From cppreference.com
< cpp‎ | language
Revision as of 23:05, 31 May 2013 by P12bot (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
 

Specifies the alignment requirement of a type or an object.

Contents

Syntax

alignas( expression ) (since C++11)
alignas( type-id ) (since C++11)

Explanation

The alignas specifier may be applied to the declaration of a variable or a class data member, or it can be applied to the definition of a class/struct/union or enum.

When used in form alignas(expression), and expression is an integral constant expression that evaluates to a positive value, the declared entity will have its alignment requirement set to exactly the result of the expression, except if it would weaken the natural alignment requirement of the type.

When used in form alignas(type), is exactly equivalent to alignas(alignof(type)), that is, the alignment requirement of the declared entity will be equal the alignment requirement of type

Notes

alignas(0) has no effect.

When multiple alignas specifiers are applied to the same variable or class, the strictest one is used.

The C language defines alignas as a preprocessor macro in the header <stdalign.h>, but in C++ this is a keyword, and the headers <stdalign.h> and <cstdalign> do not define such macro. They do, however, define the macro constant __alignas_is_defined.

Keywords

alignas

Example

// every object of type sse_t will be aligned to 16-byte boundary
struct alignas(16) sse_t
{
  float sse_data[4];
};
 
// the array "cacheline" will be aligned to 128-byte boundary
alignas(128) char cacheline[128];



See also

alignof operator queries alignment requirements of a type (since C++11) [edit]
obtains the type's alignment requirements
(class template) [edit]