Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::alignment_of

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (C++11)(until C++20*)
      (C++11)(deprecated in C++20)
      (C++11)
      Type trait constants
      Metafunctions
      (C++17)
      Supported operations
      Relationships and property queries
      (C++11)
      alignment_of
      (C++11)
      (C++11)
      (C++11)
      Type modifications
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      Compile-time rational arithmetic
      Compile-time integer sequences
       
      Defined in header<type_traits>
      template<class T>
      struct alignment_of;
      (since C++11)

      Provides the member constantvalue equal to thealignment requirement of the typeT, as if obtained by analignof expression. IfT is an array type, returns the alignment requirements of the element type. IfT is a reference type, returns the alignment requirements of the type referred to.

      Ifalignof(T) is not a valid expression, the behavior is undefined.

      If the program adds specializations forstd::alignment_of orstd::alignment_of_v(since C++17), the behavior is undefined.

      Contents

      [edit] Helper variable template

      template<class T>
      constexprstd::size_t alignment_of_v= alignment_of<T>::value;
      (since C++17)

      Inherited fromstd::integral_constant

      Member constants

      value
      [static]
      alignof(T)
      (public static member constant)

      Member functions

      operator std::size_t
      converts the object tostd::size_t, returnsvalue
      (public member function)
      operator()
      (C++14)
      returnsvalue
      (public member function)

      Member types

      Type Definition
      value_typestd::size_t
      typestd::integral_constant<std::size_t, value>

      [edit]Possible implementation

      template<class T>struct alignment_of:std::integral_constant<std::size_t, alignof(T)>{};

      [edit]Notes

      This type trait predates thealignof keyword, which can be used to obtain the same value with less verbosity.

      [edit]Example

      Run this code
      #include <cstdint>#include <iostream>#include <type_traits> struct A{};struct B{std::int8_t p;std::int16_t q;}; int main(){std::cout<< std::alignment_of<A>::value<<' ';std::cout<< std::alignment_of<B>::value<<' ';std::cout<< std::alignment_of<int>()<<' ';// alt syntaxstd::cout<< std::alignment_of_v<double><<'\n';// c++17 alt syntax}

      Possible output:

      1 2 4 8

      [edit]See also

      alignof(C++11) queries alignment requirements of a type
      (operator)[edit]
      alignas(C++11) specifies that the storage for the variable should be aligned by specific amount
      (specifier)[edit]
      (since C++11)(deprecated in C++23)
      defines the type suitable for use as uninitialized storage for types of given size
      (class template)[edit]
      (since C++11)(deprecated in C++23)
      defines the type suitable for use as uninitialized storage for all given types
      (class template)[edit]
      trivial type with alignment requirement as great as any other scalar type
      (typedef)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/alignment_of&oldid=176426"

      [8]ページ先頭

      ©2009-2025 Movatter.jp