Namespaces
Variants

std:: integral_constant

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
integral_constant bool_constant true_type false_type
(C++11) (C++17) (C++11) (C++11)
Metafunctions
(C++17)
Supported operations
Relationships and property queries
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, T v >
struct integral_constant ;
(C++11부터)

std::integral_constant 는 지정된 타입의 정적 상수를 감싸는 클래스입니다. 이것은 C++ 타입 특성(traits)의 기본 클래스입니다.

프로그램이 std::integral_constant 에 대한 특수화를 추가하는 경우, 동작은 정의되지 않습니다.

목차

헬퍼 별칭 템플릿

도우미 별칭 템플릿 std::bool_constant T bool 인 일반적인 경우를 위해 정의됩니다.

template < bool B >
using bool_constant = integral_constant < bool , B > ;
(C++17부터)

특수화

T bool 인 일반적인 경우를 위한 두 가지 typedef가 제공됩니다:

헤더에 정의됨 <type_traits>
이름 정의
true_type std :: integral_constant < bool , true >
false_type std :: integral_constant < bool , false >

멤버 타입

이름 정의
value_type T
type std :: integral_constant < T, v >

멤버 상수

이름
constexpr T value
[static]
v
(public static member constant)

멤버 함수

operator value_type
래핑된 값을 반환합니다
(public member function)
operator()
(C++14)
래핑된 값을 반환합니다
(public member function)

std::integral_constant:: operator value_type

constexpr operator value_type ( ) const noexcept ;

변환 함수. 감싸진 값을 반환합니다.

std::integral_constant:: operator()

constexpr value_type operator ( ) ( ) const noexcept ;
(C++14부터)

래핑된 값을 반환합니다. 이 함수는 std::integral_constant 이 컴파일 타임 함수 객체의 소스로 사용될 수 있도록 합니다.

가능한 구현

template<class T, T v>
struct integral_constant
{
    static constexpr T value = v;
    using value_type = T;
    using type = integral_constant; // 주입된 클래스 이름 사용
    constexpr operator value_type() const noexcept { return value; }
    constexpr value_type operator()() const noexcept { return value; } // C++14 이후
};

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_integral_constant_callable 201304L (C++14) std::integral_constant::operator()
__cpp_lib_bool_constant 201505L (C++17) std::bool_constant

예제

#include <type_traits>
using two_t = std::integral_constant<int, 2>;
using four_t = std::integral_constant<int, 4>;
static_assert(not std::is_same_v<two_t, four_t>);
static_assert(two_t::value * 2 == four_t::value, "2*2 != 4");
static_assert(two_t() << 1 == four_t() >> 0, "2*2 != 4");
enum class E{ e1, e2 };
using c1 = std::integral_constant<E, E::e1>;
using c2 = std::integral_constant<E, E::e2>;
static_assert(c1::value != E::e2);
static_assert(c1() == E::e1);
static_assert(std::is_same_v<c2, c2>);
int main() {}

참고 항목

정수 컴파일 타임 시퀀스를 구현함
(클래스 템플릿)