BDE 4.14.0 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.
118template <class t_TYPE, bool t_ADD_CONST_FLAG>
120
121 // PUBLIC TYPES
122
123 /// This `typedef` is an alias to a type that is the same as the
124 /// (template parameter) `t_TYPE` except that a top-level
125 /// `const`-qualifier has been added.
126 typedef t_TYPE const Type;
127};
128
129 // ==================================
130 // struct AddConst_Imp<t_TYPE, false>
131 // ==================================
132
133/// This partial specialization of `AddConst_Imp`, for when the (template
134/// parameter) `t_ADD_CONST_FLAG` is `false`, provides an alias `Type` that
135/// has the same type as the (template parameter) `t_TYPE`.
136template <class t_TYPE>
137struct AddConst_Imp<t_TYPE, false> {
138
139 // PUBLIC TYPES
140
141 /// This `typedef` is an alias to the (template parameter) `t_TYPE`.
142 typedef t_TYPE Type;
143};
144
145} // close package namespace
146
147
148namespace bsl {
149
150 // ================
151 // struct add_const
152 // ================
153
154/// This `struct` template implements the @ref add_const meta-function defined
155/// in the C++11 standard [meta.trans.cv], providing an alias, `type`, that
156/// returns the result. If the (template parameter) `t_TYPE` is not a
157/// reference type, nor a function type, nor already `const`-qualified at
158/// the top-level, then `type` is an alias to `t_TYPE` with a top-level
159/// `const`-qualifier added; otherwise, `type` is an alias to `t_TYPE`.
160template <class t_TYPE>
161struct add_const {
162
163 // PUBLIC TYPES
164
165 /// This `typedef` is an alias to the (template parameter) `t_TYPE` with
166 /// a top-level `const`-qualifier added if `t_TYPE` is not a reference,
167 /// nor a function, nor already `const`-qualified; otherwise, `type` is
168 /// an alias to `t_TYPE`.
169 typedef typename BloombergLP::bslmf::AddConst_Imp<
170 t_TYPE,
173};
174
175#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
176
177// ALIASES
178
179/// `add_const_t` is an alias to the return type of the @ref add_const
180/// meta-function. Note, that the `add_const_t` avoids the `::type` suffix
181/// and `typename` prefix when we want to use the result of the
182/// meta-function in templates.
183template <class t_TYPE>
184using add_const_t = typename add_const<t_TYPE>::type;
185#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
186
187} // close namespace bsl
188
189#endif
190
191// ----------------------------------------------------------------------------
192// Copyright 2013 Bloomberg Finance L.P.
193//
194// Licensed under the Apache License, Version 2.0 (the "License");
195// you may not use this file except in compliance with the License.
196// You may obtain a copy of the License at
197//
198// http://www.apache.org/licenses/LICENSE-2.0
199//
200// Unless required by applicable law or agreed to in writing, software
201// distributed under the License is distributed on an "AS IS" BASIS,
202// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203// See the License for the specific language governing permissions and
204// limitations under the License.
205// ----------------------------- END-OF-FILE ----------------------------------
206
207/** @} */
208/** @} */
209/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_printmethods.h:283
Definition bdlbb_blob.h:576
Definition bslmf_addconst.h:161
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:172
Definition bslmf_isconst.h:144
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:142
Definition bslmf_addconst.h:119
t_TYPE const Type
Definition bslmf_addconst.h:126