BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_addconst.h
Go to the documentation of this file.
1/// @file bslmf_addconst.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_addconst.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ADDCONST
9#define INCLUDED_BSLMF_ADDCONST
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_addconst bslmf_addconst
15/// @brief Provide a meta-function for adding a top-level `const`-qualifier.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_addconst
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_addconst-purpose"> Purpose</a>
25/// * <a href="#bslmf_addconst-classes"> Classes </a>
26/// * <a href="#bslmf_addconst-description"> Description </a>
27/// * <a href="#bslmf_addconst-usage"> Usage </a>
28/// * <a href="#bslmf_addconst-example-1-adding-a-const-qualifier-to-a-type"> Example 1: Adding a const-qualifier to a Type </a>
29///
30/// # Purpose {#bslmf_addconst-purpose}
31/// Provide a meta-function for adding a top-level `const`-qualifier.
32///
33/// # Classes {#bslmf_addconst-classes}
34///
35/// - bsl::add_const: meta-function for adding a top-level `const`-qualifier
36/// - bsl::add_const_t: alias to the return type of the meta-function
37///
38/// @see bslmf_removeconst
39///
40/// # Description {#bslmf_addconst-description}
41/// This component defines a meta-function, `bsl::add_const` and
42/// declares an `bsl::add_const_t` alias to the return type of the
43/// `bsl::add_const`, that may be used to add a top-level `const`-qualifier to a
44/// type if it is not a reference type, nor a function type, nor already
45/// `const`-qualified at the top-level.
46///
47/// `bsl::add_const` and `bsl::add_const_t` meet the requirements of the
48/// @ref add_const template defined in the C++11 standard [meta.trans.cv].
49///
50/// ## Usage {#bslmf_addconst-usage}
51///
52///
53/// In this section we show intended use of this component.
54///
55/// ### Example 1: Adding a const-qualifier to a Type {#bslmf_addconst-example-1-adding-a-const-qualifier-to-a-type}
56///
57///
58/// Suppose that we want to add a `const`-qualifier to a particular type.
59///
60/// First, we create two `typedef`s -- a `const`-qualified type (`MyConstType`)
61/// and the same type without the `const`-qualifier (`MyType`):
62/// @code
63/// typedef int MyType;
64/// typedef const int MyConstType;
65/// @endcode
66/// Now, we add a `const`-qualifier to `MyType` using `bsl::add_const` and
67/// verify that the resulting type is the same as `MyConstType`:
68/// @code
69/// assert(true ==
70/// (bsl::is_same<bsl::add_const<MyType>::type, MyConstType>::value));
71/// @endcode
72/// Finally, if the current compiler supports alias templates C++11 feature, we
73/// add a `const`-qualifier to `MyType` using `bsl::add_const_t` and verify that
74/// the resulting type is the same as `MyConstType`:
75/// @code
76/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
77/// assert(true ==
78/// (bsl::is_same<bsl::add_const_t<MyType>, MyConstType>::value));
79/// #endif
80/// @endcode
81/// Note, that the `bsl::add_const_t` avoids the `::type` suffix and `typename`
82/// prefix when we want to use the result of the `bsl::add_const` meta-function
83/// in templates.
84/// @}
85/** @} */
86/** @} */
87
88/** @addtogroup bsl
89 * @{
90 */
91/** @addtogroup bslmf
92 * @{
93 */
94/** @addtogroup bslmf_addconst
95 * @{
96 */
97
98#include <bslscm_version.h>
99
100#include <bslmf_isconst.h>
101#include <bslmf_isfunction.h>
102#include <bslmf_isreference.h>
103
105
106
107namespace bslmf {
108
109 // ===================
110 // struct AddConst_Imp
111 // ===================
112
113/// This `struct` template provides an alias `Type` that adds a
114/// `const`-qualifier to the (template parameter) `t_TYPE` if the (template
115/// parameter) `t_ADD_CONST_FLAG` is `true`. This generic default template
116/// adds the `const`-qualifier to `t_TYPE` in the `Type` alias. A template
117/// specialization (below) leaves `t_TYPE` as-is in its `Type` alias.
118///
119/// See @ref bslmf_addconst
120template <class t_TYPE, bool t_ADD_CONST_FLAG>
122
123 // PUBLIC TYPES
124
125 /// This `typedef` is an alias to a type that is the same as the
126 /// (template parameter) `t_TYPE` except that a top-level
127 /// `const`-qualifier has been added.
128 typedef t_TYPE const Type;
129};
130
131 // ==================================
132 // struct AddConst_Imp<t_TYPE, false>
133 // ==================================
134
135/// This partial specialization of `AddConst_Imp`, for when the (template
136/// parameter) `t_ADD_CONST_FLAG` is `false`, provides an alias `Type` that
137/// has the same type as the (template parameter) `t_TYPE`.
138template <class t_TYPE>
139struct AddConst_Imp<t_TYPE, false> {
140
141 // PUBLIC TYPES
142
143 /// This `typedef` is an alias to the (template parameter) `t_TYPE`.
144 typedef t_TYPE Type;
145};
146
147} // close package namespace
148
149
150namespace bsl {
151
152 // ================
153 // struct add_const
154 // ================
155
156/// This `struct` template implements the @ref add_const meta-function defined
157/// in the C++11 standard [meta.trans.cv], providing an alias, `type`, that
158/// returns the result. If the (template parameter) `t_TYPE` is not a
159/// reference type, nor a function type, nor already `const`-qualified at
160/// the top-level, then `type` is an alias to `t_TYPE` with a top-level
161/// `const`-qualifier added; otherwise, `type` is an alias to `t_TYPE`.
162///
163/// See @ref bslmf_addconst
164template <class t_TYPE>
165struct add_const {
166
167 // PUBLIC TYPES
168
169 /// This `typedef` is an alias to the (template parameter) `t_TYPE` with
170 /// a top-level `const`-qualifier added if `t_TYPE` is not a reference,
171 /// nor a function, nor already `const`-qualified; otherwise, `type` is
172 /// an alias to `t_TYPE`.
173 typedef typename BloombergLP::bslmf::AddConst_Imp<
174 t_TYPE,
177};
178
179#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
180
181// ALIASES
182
183/// @ref add_const_t is an alias to the return type of the @ref add_const
184/// meta-function. Note, that the @ref add_const_t avoids the `::type` suffix
185/// and `typename` prefix when we want to use the result of the
186/// meta-function in templates.
187template <class t_TYPE>
188using add_const_t = typename add_const<t_TYPE>::type;
189#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
190
191} // close namespace bsl
192
193#endif
194
195// ----------------------------------------------------------------------------
196// Copyright 2013 Bloomberg Finance L.P.
197//
198// Licensed under the Apache License, Version 2.0 (the "License");
199// you may not use this file except in compliance with the License.
200// You may obtain a copy of the License at
201//
202// http://www.apache.org/licenses/LICENSE-2.0
203//
204// Unless required by applicable law or agreed to in writing, software
205// distributed under the License is distributed on an "AS IS" BASIS,
206// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
207// See the License for the specific language governing permissions and
208// limitations under the License.
209// ----------------------------- END-OF-FILE ----------------------------------
210
211/** @} */
212/** @} */
213/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlat_valuetypefunctions.h:939
Definition bdlbb_blob.h:579
Definition bslmf_addconst.h:165
BloombergLP::bslmf::AddConst_Imp< t_TYPE,!is_reference< t_TYPE >::value &&!is_function< t_TYPE >::value &&!is_const< t_TYPE >::value >::Type type
Definition bslmf_addconst.h:176
Definition bslmf_isconst.h:145
Definition bslmf_isfunction.h:232
Definition bslmf_isreference.h:137
t_TYPE Type
This typedef is an alias to the (template parameter) t_TYPE.
Definition bslmf_addconst.h:144
Definition bslmf_addconst.h:121
t_TYPE const Type
Definition bslmf_addconst.h:128