Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/types/conditional"

From cppreference.com
< cpp‎ | types
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/types/navbar}}
+
{{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 { typedef T type; };
+
struct conditional { using type = T; };
  
 
template<class T, class F>
 
template<class T, class F>
struct conditional<false, T, F> { typedef F type; };
+
struct conditional<false, T, F> { using type = F; };
 
}}
 
}}
  
Line 44: Line 46:
 
int main()  
 
int main()  
 
{
 
{
     typedef std::conditional<true, int, double>::type Type1;
+
     using Type1 = std::conditional<true, int, double>::type;
     typedef std::conditional<false, int, double>::type Type2;
+
     using Type2 = std::conditional<false, int, double>::type;
     typedef std::conditional<sizeof(int) >= sizeof(double), int, double>::type Type3;
+
     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}}
  
[[de:cpp/types/conditional]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/types/conditional]]
+
[[fr:cpp/types/conditional]]
+
[[it:cpp/types/conditional]]
+
[[ja:cpp/types/conditional]]
+
[[pt:cpp/types/conditional]]
+
[[ru:cpp/types/conditional]]
+
[[zh:cpp/types/conditional]]
+

Latest revision as of 01:44, 5 June 2023

 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++14)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20*)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)
(C++17)

conditional
(C++11)
(C++11)(until C++20*)(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 
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

#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) [edit]