Difference between revisions of "cpp/types/conditional"
From cppreference.com
D41D8CD98F (Talk | contribs) m |
m (Create type alias in the example with using instead of typedef) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|conditional}} | {{cpp/title|conditional}} | ||
− | {{cpp/ | + | {{cpp/meta/navbar}} |
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header | type_traits}} | {{dcl header | type_traits}} | ||
Line 10: | Line 10: | ||
Provides member typedef {{tt|type}}, which is defined as {{tt|T}} if {{tt|B}} is {{c|true}} at compile time, or as {{tt|F}} if {{tt|B}} is {{c|false}}. | Provides member typedef {{tt|type}}, which is defined as {{tt|T}} if {{tt|B}} is {{c|true}} at compile time, or as {{tt|F}} if {{tt|B}} is {{c|false}}. | ||
+ | |||
+ | {{cpp/types/nospec}} | ||
===Member types=== | ===Member types=== | ||
Line 29: | Line 31: | ||
| 1= | | 1= | ||
template<bool B, class T, class F> | template<bool B, class T, class F> | ||
− | struct conditional { | + | struct conditional { using type = T; }; |
template<class T, class F> | template<class T, class F> | ||
− | struct conditional<false, T, F> { | + | struct conditional<false, T, F> { using type = F; }; |
}} | }} | ||
Line 44: | Line 46: | ||
int main() | 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(Type1).name() << '\n'; | ||
Line 64: | Line 66: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 01:44, 5 June 2023
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 |
[edit] Member types
Member type | Definition |
type
|
T if B == true, F if B == false
|
[edit] Helper types
template< bool B, class T, class F > using conditional_t = typename conditional<B,T,F>::type; |
(since C++14) | |
[edit] Possible implementation
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; }; |
[edit] Example
Run this code
#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'; }
Possible output:
int double double
[edit] See also
(C++11) |
conditionally removes a function overload or template specialization from overload resolution (class template) |