types_integral_constant (정수 상수 타입)
이 페이지에서는 C++ 표준 라이브러리의 std::integral_constant에 대해 설명해요. std::integral_constant는 지정된 타입의 정적 상수를 감싸는 래퍼로, C++ 타입 특성(type traits)의 기반 클래스로 사용돼요. 프로그램에서 std::integral_constant에 대한 특수화를 추가하면 동작이 정의되지 않아요.
출처: cppreference
본문
정의
<type_traits> 헤더에 정의됨 |
|
|
template < class T , T v > struct integral_constant ; |
|
(C++11부터) |
std::integral_constant는 지정된 타입의 정적 상수를 래핑해요. C++ 타입 특성들의 기반 클래스이기도 해요.
헬퍼 별칭 템플릿
T가 bool인 일반적인 경우를 위해 헬퍼 별칭 템플릿 std::bool_constant가 정의되어 있어요.
template < bool B > using bool_constant = integral_constant < bool , B > ; |
|
(C++17부터) |
특수화
T가 bool인 일반적인 경우를 위해 두 가지 typedef가 제공돼요.
<type_traits> 헤더에 정의됨 |
| 이름 |
true_type |
false_type |
멤버 타입
| 이름 |
정의 |
value_type |
T |
type |
std :: integral_constant < T , v > |
멤버 상수
| 이름 |
값 |
value [static] |
v (공용 정적 멤버 상수) |
멤버 함수
operator value_type |
래핑된 값을 반환해요 (공용 멤버 함수) |
operator() (C++14) |
래핑된 값을 반환해요 (공용 멤버 함수) |
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; // using injected-class-name
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; } // since 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() {}
같이 보기
integer_sequence (C++14) |
컴파일 타임 정수 시퀀스를 구현해요 (클래스 템플릿) |
더 알아보기 (Learn more)
cppreference