std::conditional
Определён в заголовочном файле <type_traits> | ||
|---|---|---|
template< bool B, class T, class F > struct conditional; | (since 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; | (since 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) | условно удаляет перегрузку функции или специализацию шаблона из множества кандидатов при разрешении перегрузки (шаблон класса) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/types/conditional