BDE 4.14.0 Production release
|
Provide a compile-time conditional type selector.
bsl::conditional
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.
In this section we show intended use of this component.
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
:
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
:
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.