Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      alignas specifier(since C++11)

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      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
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Declarations
       

      Specifies thealignment requirement of a type or an object.

      Contents

      [edit]Syntax

      alignas(expression)
      alignas(type-id)
      alignas(pack...)
      1)expression must be anintegral constant expression that evaluates to zero, or to a valid value for analignment or extended alignment.
      2) Equivalent toalignas(alignof(type-id)).
      3) Equivalent to multiple alignas specifiers applied to the same declaration, one for each member of theparameter pack, which can be either type or constant parameter pack.

      [edit]Explanation

      Thealignas specifier may be applied to:

      • the declaration or definition of aclass;
      • the declaration of a non-bitfield class data member;
      • the declaration of a variable, except that it cannot be applied to the following:
        • a function parameter;
        • the exception parameter of a catch clause.

      The object or the type declared by such a declaration will have itsalignment requirement equal to the strictest (largest) non-zeroexpression of allalignas specifiers used in the declaration, unless it would weaken the natural alignment of the type.

      If the strictest (largest)alignas on a declaration is weaker than the alignment it would have without anyalignas specifiers (that is, weaker than its natural alignment or weaker thanalignas on another declaration of the same object or type), the program is ill-formed:

      struct alignas(8) S{};struct alignas(1) U{ S s;};// error: alignment of U would have been 8 without alignas(1)

      Invalid non-zero alignments, such asalignas(3) are ill-formed.

      Valid non-zero alignments that are weaker than anotheralignas on the same declaration are ignored.

      alignas(0) is always ignored.

      [edit]Notes

      As of the ISO C11 standard, the C language has the_Alignas keyword and definesalignas as a preprocessor macro expanding to the keyword in the header<stdalign.h>.

      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.

      (until C++20)

      the header<stdalign.h> does not define such macro. It does, however, define the macro constant__alignas_is_defined.

      (since C++20)

      [edit]Keywords

      alignas

      [edit]Example

      Run this code
      #include <iostream> // Every object of type struct_float will be aligned// to alignof(float) boundary (usually 4):struct alignas(float) struct_float{// your definition here}; // Every object of type sse_t will be aligned to 32-byte boundary:struct alignas(32) sse_t{float sse_data[4];}; int main(){struct default_aligned{float data[4];} a, b, c;    sse_t x, y, z; std::cout<<"alignof(struct_float) = "<< alignof(struct_float)<<'\n'<<"sizeof(sse_t) = "<< sizeof(sse_t)<<'\n'<<"alignof(sse_t) = "<< alignof(sse_t)<<'\n'<<std::hex<<std::showbase<<"&a: "<<&a<<"\n""&b: "<<&b<<"\n""&c: "<<&c<<"\n""&x: "<<&x<<"\n""&y: "<<&y<<"\n""&z: "<<&z<<'\n';}

      Possible output:

      alignof(struct_float) = 4sizeof(sse_t) = 32alignof(sse_t) = 32&a: 0x7fffcec89930&b: 0x7fffcec89940&c: 0x7fffcec89950&x: 0x7fffcec89960&y: 0x7fffcec89980&z: 0x7fffcec899a0

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1437C++11alignas could be used in alias declarationsprohibited
      CWG 2354C++11alignas could be applied to the declaration of an enumerationprohibited

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 9.12.4 Carries dependency attribute [dcl.attr.depend]
      • C++20 standard (ISO/IEC 14882:2020):
      • 9.12.3 Carries dependency attribute [dcl.attr.depend]
      • C++17 standard (ISO/IEC 14882:2017):
      • 10.6.3 Carries dependency attribute [dcl.attr.depend]
      • C++14 standard (ISO/IEC 14882:2014):
      • 7.6.4 Carries dependency attribute [dcl.attr.depend]
      • C++11 standard (ISO/IEC 14882:2011):
      • 7.6.4 Carries dependency attribute [dcl.attr.depend]

      [edit]See also

      alignof(C++11) queries alignment requirements of a type
      (operator)[edit]
      obtains the type's alignment requirements
      (class template)[edit]
      C documentation for_Alignas, alignas
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/alignas&oldid=182742"

      [8]ページ先頭

      ©2009-2025 Movatter.jp