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)

Предоставляет определение типа-члена type , которое определяется как T если B равно true во время компиляции, или как F если B равно false .

Если программа добавляет специализации для 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)
условно удаляет перегрузку функции или специализацию шаблона из разрешения перегрузки
(шаблон класса)