BDE 4.14.0 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
115/// the (template parameter) type `t_FALSE_TYPE`. Note that this generic
116/// default template defines `type` to be an alias to `t_TRUE_TYPE` for when
117/// `t_COND` is `true`. A template specialization is provided (below)
118/// handles the case for when `t_COND` is `false`.
119template <bool t_COND, class t_TRUE_TYPE, class t_FALSE_TYPE>
121
122 /// This `typedef` is an alias to the (template parameter) type
123 /// `t_TRUE_TYPE`.
124 typedef t_TRUE_TYPE type;
125};
126
127/// This partial specialization of `bsl::conditional`, for when the
128/// (template parameter) value `t_COND` is `false`, provides a `typedef`
129/// `type` that is an alias to the (template parameter) type `t_FALSE_TYPE`.
130template <class t_TRUE_TYPE, class t_FALSE_TYPE>
131struct conditional<false, t_TRUE_TYPE, t_FALSE_TYPE> {
132
133 /// This `typedef` is an alias to the (template parameter) type
134 /// `t_FALSE_TYPE`.
135 typedef t_FALSE_TYPE type;
136};
137
138#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
139
140// ALIASES
141
142/// `conditional_t` is an alias to the return type of the `bsl::conditional`
143/// meta-function. Note, that the `conditional_t` avoids the `::type`
144/// suffix and `typename` prefix when we want to use the result of the
145/// meta-function in templates.
146template <bool t_COND, class t_TRUE_TYPE, class t_FALSE_TYPE>
147using conditional_t =
149
150#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
151
152} // close namespace bsl
153
154#endif
155
156// ----------------------------------------------------------------------------
157// Copyright 2013 Bloomberg Finance L.P.
158//
159// Licensed under the Apache License, Version 2.0 (the "License");
160// you may not use this file except in compliance with the License.
161// You may obtain a copy of the License at
162//
163// http://www.apache.org/licenses/LICENSE-2.0
164//
165// Unless required by applicable law or agreed to in writing, software
166// distributed under the License is distributed on an "AS IS" BASIS,
167// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
168// See the License for the specific language governing permissions and
169// limitations under the License.
170// ----------------------------- END-OF-FILE ----------------------------------
171
172/** @} */
173/** @} */
174/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_printmethods.h:283
t_FALSE_TYPE type
Definition bslmf_conditional.h:135
Definition bslmf_conditional.h:120
t_TRUE_TYPE type
Definition bslmf_conditional.h:124
Definition bslmf_integralconstant.h:244