Quick Links:

bal | bbl | bdl | bsl

Classes

Component bslmf_conditional
[Package bslmf]

Provide a compile-time conditional type selector. More...

Classes

struct  bsl::conditional< t_COND, t_TRUE_TYPE, t_FALSE_TYPE >
struct  bsl::conditional< false, t_TRUE_TYPE, t_FALSE_TYPE >

Detailed Description

Outline
Purpose:
Provide a compile-time conditional type selector.
Classes:
bsl::conditional standard meta-function for conditional type selection
bsl::conditional_t alias to the return type of the bsl::conditional
See also:
Component bslmf_enableif
Description:
This component defines a meta-function, bsl::conditional, that may be used to conditionally select one of its two (template parameter) types based on a bool (template parameter) value.
bsl::conditional meets the requirements of the conditional template defined in the C++11 standard [meta.trans.other], providing a typedef type that is an alias to the first (template parameter) type if the (template parameter) value is true; otherwise, type is an alias to the second (template parameter) type.
Usage:
In this section we show intended use of this component.
Example 1: Conditionally Select From Two Types:
Suppose that we want to select between two types based on a bool value.
Now, we use bsl::conditional to select between two types, int and char, with a bool value. When the bool is true, we select int; otherwise, we select char. We verify that our code behaves correctly by asserting the result of the bsl::conditional with the expected type using bsl::is_same:
  assert(true ==
      (bsl::is_same<bsl::conditional<true,  int, char>::type, int >::value));
  assert(true ==
      (bsl::is_same<bsl::conditional<false, int, char>::type, char>::value));
Finally, if the current compiler supports alias templates C++11 feature, we select between two types using bsl::conditional_t and verify that our code behaves correctly by asserting the result of the bsl::conditional_t with the expected type using bsl::is_same:
#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
  assert(true ==
          (bsl::is_same<bsl::conditional_t<true,  int, char>, int >::value));
  assert(true ==
          (bsl::is_same<bsl::conditional_t<false, int, char>, char>::value));
#endif
Note, that the bsl::conditional_t avoids the ::type suffix and typename prefix when we want to use the result of the bsl::conditional meta-function in templates.