BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_conditional.h
Go to the documentation of this file.
1/// @file bslmf_conditional.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_conditional.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_CONDITIONAL
9#define INCLUDED_BSLMF_CONDITIONAL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_conditional bslmf_conditional
15/// @brief Provide a compile-time conditional type selector.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_conditional
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_conditional-purpose"> Purpose</a>
25/// * <a href="#bslmf_conditional-classes"> Classes </a>
26/// * <a href="#bslmf_conditional-description"> Description </a>
27/// * <a href="#bslmf_conditional-usage"> Usage </a>
28/// * <a href="#bslmf_conditional-example-1-conditionally-select-from-two-types"> Example 1: Conditionally Select From Two Types </a>
29///
30/// # Purpose {#bslmf_conditional-purpose}
31/// Provide a compile-time conditional type selector.
32///
33/// # Classes {#bslmf_conditional-classes}
34///
35/// - bsl::conditional: standard meta-function for conditional type selection
36/// - bsl::conditional_t: alias to the return type of the `bsl::conditional`
37///
38/// @see bslmf_enableif
39///
40/// # Description {#bslmf_conditional-description}
41/// This component defines a meta-function, `bsl::conditional`,
42/// that may be used to conditionally select one of its two (template parameter)
43/// types based on a `bool` (template parameter) value.
44///
45/// `bsl::conditional` meets the requirements of the `conditional` template
46/// defined in the C++11 standard [meta.trans.other], providing a `typedef`
47/// `type` that is an alias to the first (template parameter) type if the
48/// (template parameter) value is `true`; otherwise, `type` is an alias to the
49/// second (template parameter) type.
50///
51/// ## Usage {#bslmf_conditional-usage}
52///
53///
54/// In this section we show intended use of this component.
55///
56/// ### Example 1: Conditionally Select From Two Types {#bslmf_conditional-example-1-conditionally-select-from-two-types}
57///
58///
59/// Suppose that we want to select between two types based on a `bool` value.
60///
61/// Now, we use `bsl::conditional` to select between two types, `int` and
62/// `char`, with a `bool` value. When the `bool` is `true`, we select `int`;
63/// otherwise, we select `char`. We verify that our code behaves correctly by
64/// asserting the result of the `bsl::conditional` with the expected type using
65/// `bsl::is_same`:
66/// @code
67/// assert(true ==
68/// (bsl::is_same<bsl::conditional<true, int, char>::type, int >::value));
69/// assert(true ==
70/// (bsl::is_same<bsl::conditional<false, int, char>::type, char>::value));
71/// @endcode
72/// Finally, if the current compiler supports alias templates C++11 feature, we
73/// select between two types using `bsl::conditional_t` and verify that our code
74/// behaves correctly by asserting the result of the `bsl::conditional_t` with
75/// the expected type using `bsl::is_same`:
76/// @code
77/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
78/// assert(true ==
79/// (bsl::is_same<bsl::conditional_t<true, int, char>, int >::value));
80/// assert(true ==
81/// (bsl::is_same<bsl::conditional_t<false, int, char>, char>::value));
82/// #endif
83/// @endcode
84/// Note, that the `bsl::conditional_t` avoids the `::type` suffix and
85/// `typename` prefix when we want to use the result of the `bsl::conditional`
86/// meta-function in templates.
87/// @}
88/** @} */
89/** @} */
90
91/** @addtogroup bsl
92 * @{
93 */
94/** @addtogroup bslmf
95 * @{
96 */
97/** @addtogroup bslmf_conditional
98 * @{
99 */
100
101#include <bslscm_version.h>
102
104
105namespace bsl {
106 // ==================
107 // struct conditional
108 // ==================
109
110/// This `struct` template implements the `conditional` meta-function
111/// defined in the C++ standard [meta.trans.other], providing an alias,
112/// `type`, that returns the result. If the (template parameter) value
113/// `t_COND` is `true`, then `type` has the same type as the (template
114/// parameter) type `t_TRUE_TYPE`; otherwise, `type` has the same type as the (template parameter) type `t_FALSE_TYPE`.
115///
116/// \note Note that this generic
117/// default template defines `type` to be an alias to `t_TRUE_TYPE` for when
118/// `t_COND` is `true`. A template specialization is provided (below)
119/// handles the case for when `t_COND` is `false`.
120///
121/// See @ref bslmf_conditional
122template <bool t_COND, class t_TRUE_TYPE, class t_FALSE_TYPE>
124
125 /// This `typedef` is an alias to the (template parameter) type
126 /// `t_TRUE_TYPE`.
127 typedef t_TRUE_TYPE type;
128};
129
130/// This partial specialization of `bsl::conditional`, for when the
131/// (template parameter) value `t_COND` is `false`, provides a `typedef`
132/// `type` that is an alias to the (template parameter) type `t_FALSE_TYPE`.
133template <class t_TRUE_TYPE, class t_FALSE_TYPE>
134struct conditional<false, t_TRUE_TYPE, t_FALSE_TYPE> {
135
136 /// This `typedef` is an alias to the (template parameter) type
137 /// `t_FALSE_TYPE`.
138 typedef t_FALSE_TYPE type;
139};
140
141#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
142
143// ALIASES
144
145/// `conditional_t` is an alias to the return type of the `bsl::conditional`
146/// meta-function. Note, that the `conditional_t` avoids the `::type`
147/// suffix and `typename` prefix when we want to use the result of the
148/// meta-function in templates.
149template <bool t_COND, class t_TRUE_TYPE, class t_FALSE_TYPE>
150using conditional_t =
152
153#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
154
155} // close namespace bsl
156
157#endif
158
159// ----------------------------------------------------------------------------
160// Copyright 2013 Bloomberg Finance L.P.
161//
162// Licensed under the Apache License, Version 2.0 (the "License");
163// you may not use this file except in compliance with the License.
164// You may obtain a copy of the License at
165//
166// http://www.apache.org/licenses/LICENSE-2.0
167//
168// Unless required by applicable law or agreed to in writing, software
169// distributed under the License is distributed on an "AS IS" BASIS,
170// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
171// See the License for the specific language governing permissions and
172// limitations under the License.
173// ----------------------------- END-OF-FILE ----------------------------------
174
175/** @} */
176/** @} */
177/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
t_FALSE_TYPE type
Definition bslmf_conditional.h:138
Definition bslmf_conditional.h:123
t_TRUE_TYPE type
Definition bslmf_conditional.h:127
Definition bslmf_integralconstant.h:261