Difference between revisions of "cpp/types/conditional"
From cppreference.com
(~) |
m (langlinks) |
||
Line 66: | Line 66: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 11:44, 16 September 2020
Defined in header <type_traits>
|
||
template< bool B, class T, class F > struct conditional; |
(since C++11) | |
Provides member typedef type
, which is defined as T
if B
is true at compile time, or as F
if B
is false.
If the program adds specializations for std::conditional
, the behavior is undefined.
Contents |
Member types
Member type | Definition |
type
|
T if B == true, F if B == false
|
Helper types
template< bool B, class T, class F > using conditional_t = typename conditional<B,T,F>::type; |
(since C++14) | |
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'; }
Possible output:
int double double
See also
(C++11) |
conditionally removes a function overload or template specialization from overload resolution (class template) |