std::conditional

From Cppreference

Jump to: navigation, search
Defined in header <type_traits>

template< bool B, class T, class F >
struct conditional;
(C++11 feature)

Provides member typedef type, which is defined as T if B is true at compile time, or as F if B is false.

Contents

[edit] Member types

Member type Definition
type T if B == true, F if B == false

[edit] Equivalent definition


[edit] Example

#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

[edit] See also

enable_if (C++11)
hides a function overload or template specialization based on compile-time boolean
(class template)