Namespaces
Variants

std:: conditional

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
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)

conditional
(C++11)
(C++11)
(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
헤더 파일에 정의됨 <type_traits>
template < bool B, class T, class F >
struct conditional ;
(C++11부터)

멤버 typedef type 를 제공합니다. 이는 컴파일 시간에 B true 인 경우 T 로 정의되고, B false 인 경우 F 로 정의됩니다.

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

목차

멤버 타입

멤버 타입 정의
type T 만약 B == true , F 만약 B == false

헬퍼 타입

template < bool B, class T, class F >
using conditional_t = typename conditional < B,T,F > :: type ;
(C++14 이후)

가능한 구현

template<bool B, class T, class F>
struct conditional { using type = T; };
template<class T, class F>
struct conditional<false, T, F> { using type = F; };

예제

#include <iostream>
#include <type_traits>
#include <typeinfo>
int main() 
{
    using Type1 = std::conditional<true, int, double>::type;
    using Type2 = std::conditional<false, int, double>::type;
    using Type3 = std::conditional<sizeof(int) >= sizeof(double), int, double>::type;
    std::cout << typeid(Type1).name() << '\n';
    std::cout << typeid(Type2).name() << '\n';
    std::cout << typeid(Type3).name() << '\n';
}

가능한 출력:

int
double
double

참고 항목

(C++11)
오버로드 해결에서 함수 오버로드나 템플릿 특수화를 조건부로 제거 합니다
(클래스 템플릿)