Namespaces
Variants

std:: alignment_of

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
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
헤더에 정의됨 <type_traits>
template < class T >
struct alignment_of ;
(C++11부터)

멤버 상수 value 를 제공하며, 이는 정렬 요구 사항 과 동일한 값을 가지며, 마치 alignof 표현식으로 얻어진 것처럼 T 타입의 정렬을 나타냅니다. 만약 T 가 배열 타입인 경우, 요소 타입의 정렬 요구 사항을 반환합니다. 만약 T 가 참조 타입인 경우, 참조된 타입의 정렬 요구 사항을 반환합니다.

만약 alignof ( T ) 가 유효한 표현식이 아니라면, 동작은 정의되지 않습니다.

프로그램이 std::alignment_of 또는 std::alignment_of_v (C++17부터) 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

헬퍼 변수 템플릿

template < class T >
constexpr std:: size_t alignment_of_v = alignment_of < T > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

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

멤버 함수

operator std::size_t
객체를 std:: size_t 로 변환하며, value 를 반환함
(public member function)
operator()
(C++14)
value 를 반환함
(public member function)

멤버 타입

타입 정의
value_type std:: size_t
type std:: integral_constant < std:: size_t , value >

가능한 구현

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

참고 사항

이 타입 특성은 alignof 키워드보다 앞서 등장했으며, 동일한 값을 더 간결하게 얻기 위해 사용할 수 있습니다.

예제

#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>() << ' '; // 대체 구문
    std::cout << std::alignment_of_v<double> << '\n'; // C++17 대체 구문
}

가능한 출력:

1 2 4 8

참고 항목

alignof (C++11) 타입의 정렬 요구 사항을 조회함
(연산자)
alignas (C++11) 변수의 저장 공간이 특정 크기로 정렬되도록 지정함
(지정자)
(C++11부터) (C++23에서 사용 중단됨)
주어진 크기의 타입들을 위한 초기화되지 않은 저장 공간으로 사용하기 적합한 타입을 정의함
(클래스 템플릿)
(C++11부터) (C++23에서 사용 중단됨)
모든 주어진 타입들을 위한 초기화되지 않은 저장 공간으로 사용하기 적합한 타입을 정의함
(클래스 템플릿)
다른 모든 스칼라 타입만큼 큰 정렬 요구 사항을 가진 트리비얼 타입
(타입 정의)