std::alignment_of - cppreference.com (original) (raw)
| | | | | ------------------------------------------ | | ------------- | | template< class T > struct alignment_of; | | (since C++11) |
Provides the member constant value equal to the alignment requirement of the type T, as if obtained by an alignof expression. If T is an array type, returns the alignment requirements of the element type. If T is a reference type, returns the alignment requirements of the type referred to.
If alignof(T) is not a valid expression, the behavior is undefined.
If the program adds specializations for std::alignment_of or std::alignment_of_v(since C++17), the behavior is undefined.
Contents
[edit] Helper variable template
| template< class T > constexpr std::size_t alignment_of_v = alignment_of<T>::value; | | (since C++17) | | ---------------------------------------------------------------------------------------------------------- | | ------------- |
Inherited from std::integral_constant
Member constants
| | alignof(T) (public static member constant) | | --------------------------------------------- |
Member functions
| | converts the object to std::size_t, returns value (public member function) | | ----------------------------------------------------------------------------------------------- | | | returns value (public member function) |
Member types
[edit] Possible implementation
[edit] Notes
This type trait predates the alignof keyword, which can be used to obtain the same value with less verbosity.
[edit] Example
#include #include #include struct A {}; struct B { std::int8_t p; std::int16_t q; }; int main() { std::cout << std::alignment_of::value << ' '; std::cout << std::alignment_of::value << ' '; std::cout << std::alignment_of() << ' '; // alt syntax std::cout << std::alignment_of_v << '\n'; // c++17 alt syntax }
Possible output:
[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] |