A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/conditional below:

class template

<type_traits>

std::conditional
template <bool Cond, class T, class F> struct conditional;

Conditional type

Obtains either T or F, depending on whether Cond is true or false.

If Cond is true, member type conditional::type is defined as an alias of T.


If Cond is false, member type conditional::type is defined as an alias of F.

Template parameters
Cond
A compile-time constant of type bool.
T,F
Types.

Member types member type definition type If Cond is true: T
If Cond is false: F
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// conditional example
#include <iostream>
#include <type_traits>

int main() {

  short int i = 1;    // code does not compile if type of i is not integral

  typedef std::conditional<true,int,float>::type A;                      // int
  typedef std::conditional<false,int,float>::type B;                     // float
  typedef std::conditional<std::is_integral<A>::value,long,int>::type C; // long
  typedef std::conditional<std::is_integral<B>::value,long,int>::type D; // int

  std::cout << std::boolalpha;
  std::cout << "typedefs of int:" << std::endl;
  std::cout << "A: " << std::is_same<int,A>::value << std::endl;
  std::cout << "B: " << std::is_same<int,B>::value << std::endl;
  std::cout << "C: " << std::is_same<int,C>::value << std::endl;
  std::cout << "D: " << std::is_same<int,D>::value << std::endl;

  return 0;
}

Output:
typedefs of int:
A: true
B: false
C: false
D: true


See also
enable_if
Enable type if condition is met (class template)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4