std::conditional
From cppreference.com
Template:ddcl list begin <tr class="t-dsc-header">
<td>Defined in header
</td>
<type_traits>
<td></td> <td></td> </tr> <tr class="t-dcl ">
<td >template< bool B, class T, class F >
struct conditional;
</td>
struct conditional;
<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end
Provides member typedef type
, which is defined as T
if B
is true at compile time, or as F
if B
is false.
Contents |
Member types
Member type | Definition |
type
|
T if B == true, F if B == false
|
Helper types
Template:ddcl list begin <tr class="t-dcl ">
<td >template< bool B, class T, class F >
using conditional_t = typename conditional<B,T,F>::type;
</td>
using conditional_t = typename conditional<B,T,F>::type;
<td class="t-dcl-nopad"> </td> <td > (since C++14) </td> </tr> Template:ddcl list end
Possible implementation
template<bool B, class T, class F> struct conditional { typedef T type; }; template<class T, class F> struct conditional<false, T, F> { typedef F type; }; |
Example
Run this code
#include <iostream> #include <type_traits> #include <typeinfo> int main() { typedef std::conditional<true, int, double>::type Type1; typedef std::conditional<false, int, double>::type Type2; typedef std::conditional<sizeof(int) >= sizeof(double), int, double>::type Type3; std::cout << typeid(Type1).name() << '\n'; std::cout << typeid(Type2).name() << '\n'; std::cout << typeid(Type3).name() << '\n'; }
Output:
i d d
See also
(C++11) |
conditionally removes a function overload or template specialization from overload resolution (class template) |