BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_removeconst.h
Go to the documentation of this file.
1/// @file bslmf_removeconst.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_removeconst.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_REMOVECONST
9#define INCLUDED_BSLMF_REMOVECONST
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_removeconst bslmf_removeconst
15/// @brief Provide a meta-function for removing top-level `const`-qualifier.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_removeconst
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_removeconst-purpose"> Purpose</a>
25/// * <a href="#bslmf_removeconst-classes"> Classes </a>
26/// * <a href="#bslmf_removeconst-description"> Description </a>
27/// * <a href="#bslmf_removeconst-usage"> Usage </a>
28/// * <a href="#bslmf_removeconst-example-1-removing-the-const-qualifier-of-a-type"> Example 1: Removing the const-Qualifier of a Type </a>
29///
30/// # Purpose {#bslmf_removeconst-purpose}
31/// Provide a meta-function for removing top-level `const`-qualifier.
32///
33/// # Classes {#bslmf_removeconst-classes}
34///
35/// - bsl::remove_const: meta-function for removing top-level `const`-qualifier
36/// - bsl::remove_const_t: alias to the return type of the `bsl::remove_const`
37///
38/// @see bslmf_addconst
39///
40/// # Description {#bslmf_removeconst-description}
41/// This component defines a meta-function, `bsl::remove_const` and
42/// declares an `bsl::remove_const_t` alias to the return type of the
43/// `bsl::remove_const`, that may be used to remove any top-level
44/// `const`-qualifier from a type.
45///
46/// `bsl::remove_const` and `bsl::remove_const_t` meet the requirements of the
47/// @ref remove_const template defined in the C++11 standard [meta.trans.cv].
48///
49/// ## Usage {#bslmf_removeconst-usage}
50///
51///
52/// In this section we show intended use of this component.
53///
54/// ### Example 1: Removing the const-Qualifier of a Type {#bslmf_removeconst-example-1-removing-the-const-qualifier-of-a-type}
55///
56///
57/// Suppose that we want to remove any top-level `const`-qualifier from a
58/// 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 remove the `const`-qualifier from `MyConstType` using
67/// `bsl::remove_const` and verify that the resulting type is the same as
68/// `MyType`:
69/// @code
70/// assert(true ==
71/// (bsl::is_same<bsl::remove_const<MyConstType>::type, MyType>::value));
72/// @endcode
73/// Finally, if the current compiler supports alias templates C++11 feature, we
74/// remove a `const`-qualifier from `MyConstType` using `bsl::remove_const_t`
75/// and verify that the resulting type is the same as `MyType`:
76/// @code
77/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
78/// assert(true ==
79/// (bsl::is_same<bsl::remove_const_t<MyConstType>, MyType>::value));
80/// #endif
81/// @endcode
82/// Note, that the `bsl::remove_const_t` avoids the `::type` suffix and
83/// `typename` prefix when we want to use the result of the `bsl::remove_const`
84/// meta-function in templates.
85/// @}
86/** @} */
87/** @} */
88
89/** @addtogroup bsl
90 * @{
91 */
92/** @addtogroup bslmf
93 * @{
94 */
95/** @addtogroup bslmf_removeconst
96 * @{
97 */
98
99#include <bslscm_version.h>
100
102#include <bsls_platform.h>
103
104#include <stddef.h>
105
106
107// Several compiler tool-chains have problems removing the const qualifiers
108// from arrays. Additional documentation is below (where these macros are
109// used).
110#if defined(BSLS_PLATFORM_CMP_IBM)
111
112# define BSLS_REMOVECONST_WORKAROUND_CONST_MULTIDIMENSIONAL_ARRAY 1
113 // The IBM xlC compiler has an odd issue trying to remove 'const'
114 // qualifiers from multidimensional arrays. This workaround was last
115 // verified as required for the xlC 12.1 compiler - more recent compilers
116 // still need testing.
117
118#elif (defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130) \
119 || (defined(BSLS_PLATFORM_CMP_MSVC) \
120 && BSLS_PLATFORM_CMP_VERSION <= 1900 \
121 && _MSC_FULL_VER < 190023918)
122
123# define BSLS_REMOVECONST_WORKAROUND_CONST_ARRAY 1
124 // The Microsoft compiler does not recognize array-types as cv-qualified
125 // when the element type is cv-qualified when performing matching for
126 // partial template specialization, but does get the correct result when
127 // performing overload resolution for functions (taking arrays by
128 // reference). Given the function dispatch behavior being correct, we
129 // choose to work around this compiler bug, rather than try to report
130 // compiler behavior, as the compiler itself is inconsistent depending on
131 // how the trait might be used. This also corresponds to how Microsoft
132 // itself implements the trait in VC2010 and later. Note that Microsoft
133 // fixed this bug in Update 2 for MSVC2015, which requires checking the
134 // '_MSC_FULL_VER' macro; the workaround below would be ambiguous when
135 // trying to remove 'const' from an array of 'const' elements with a
136 // conforming compiler.
137
138#endif
139
140namespace bsl {
141
142 // ===================
143 // struct remove_const
144 // ===================
145
146/// This `struct` template implements the @ref remove_const meta-function
147/// defined in the C++11 standard [meta.trans.cv], providing an alias,
148/// `type`, that returns the result. `type` has the same type as the
149/// (template parameter) `t_TYPE` except that any top-level `const`-qualifier has been removed.
150///
151/// \note Note that this generic default
152/// template provides a `type` that is an alias to `t_TYPE` for when
153/// `t_TYPE` is not `const`-qualified. A template specialization is
154/// provided (below) that removes the `const`-qualifier for when `t_TYPE` is
155/// `const`-qualified.
156///
157/// See @ref bslmf_removeconst
158template <class t_TYPE>
160
161 // PUBLIC TYPES
162
163 /// This `typedef` is an alias to the (template parameter) `t_TYPE`.
164 typedef t_TYPE type;
165};
166
167 // =================================
168 // struct remove_const<t_TYPE const>
169 // =================================
170
171/// This partial specialization of `bsl::remove_const`, for when the
172/// (template parameter) `t_TYPE` is `const`-qualified, provides a
173/// `typedef`, `type`, that has the `const`-qualifier removed.
174template <class t_TYPE>
175struct remove_const<t_TYPE const> {
176
177 // PUBLIC TYPES
178
179 /// This `typedef` is an alias to the same type as the (template
180 /// parameter) `t_TYPE` except with the `const`-qualifier removed.
181 typedef t_TYPE type;
182};
183
184#if defined(BSLS_REMOVECONST_WORKAROUND_CONST_MULTIDIMENSIONAL_ARRAY)
185template <class t_TYPE, size_t N>
186struct remove_const<t_TYPE[N]> {
187 // This partial specialization of 'bsl::remove_const', for when the
188 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
189 // necessary to separately @ref remove_const on the element type, and then
190 // reconstruct the array dimensions.
191
192 // PUBLIC TYPES
193 typedef typename remove_const<t_TYPE>::type type[N];
194 // This 'typedef' is an alias to the same type as the (template
195 // parameter) 't_TYPE[N]' except with the 'const'-qualifier removed.
196};
197
198template <class t_TYPE, size_t N>
199struct remove_const<const t_TYPE[N]> {
200 // This partial specialization of 'bsl::remove_const', for when the
201 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
202 // necessary to separately @ref remove_const on the element type, and then
203 // reconstruct the array dimensions.
204
205 // PUBLIC TYPES
206 typedef typename remove_const<const t_TYPE>::type type[N];
207 // This 'typedef' is an alias to the same type as the (template
208 // parameter) 't_TYPE[N]' except with the 'const'-qualifier removed.
209};
210
211template <class t_TYPE>
212struct remove_const<t_TYPE[]> {
213 // This partial specialization of 'bsl::remove_const', for when the
214 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
215 // necessary to separately @ref remove_const on the element type, and then
216 // reconstruct the array dimensions.
217
218 // PUBLIC TYPES
219 typedef typename remove_const<t_TYPE>::type type[];
220 // This 'typedef' is an alias to the same type as the (template
221 // parameter) 't_TYPE[]' except with the 'const'-qualifier removed.
222};
223
224template <class t_TYPE>
225struct remove_const<const t_TYPE[]> {
226 // This partial specialization of 'bsl::remove_const', for when the
227 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
228 // necessary to separately @ref remove_const on the element type, and then
229 // reconstruct the array dimensions.
230
231 // PUBLIC TYPES
232 typedef typename remove_const<const t_TYPE>::type type[];
233 // This 'typedef' is an alias to the same type as the (template
234 // parameter) 't_TYPE[]' except with the 'const'-qualifier removed.
235};
236#elif defined(BSLS_REMOVECONST_WORKAROUND_CONST_ARRAY)
237template <class t_TYPE>
238struct remove_const<t_TYPE[]> {
239 // This partial specialization of 'bsl::remove_const', for when the
240 // (template parameter) 't_TYPE' is an array type. On Microsoft compilers,
241 // it is necessary to separately @ref remove_const on the element type, and
242 // then reconstruct the array dimensions.
243
244 // PUBLIC TYPES
245 typedef typename remove_const<t_TYPE>::type type[];
246 // This 'typedef' is an alias to the same type as the (template
247 // parameter) 't_TYPE[]' except with the 'const'-qualifier removed.
248};
249
250template <class t_TYPE, size_t LENGTH>
251struct remove_const<t_TYPE[LENGTH]> {
252 // This partial specialization of 'bsl::remove_const', for when the
253 // (template parameter) 't_TYPE' is an array type. On Microsoft compilers,
254 // it is necessary to separately @ref remove_const on the element type, and
255 // then reconstruct the array dimensions.
256
257 // PUBLIC TYPES
258 typedef typename remove_const<t_TYPE>::type type[LENGTH];
259 // This 'typedef' is an alias to the same type as the (template
260 // parameter) 't_TYPE[N]' except with the 'const'-qualifier removed.
261};
262#endif
263
264#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
265
266// ALIASES
267
268/// @ref remove_const_t is an alias to the return type of the
269/// `bsl::remove_const` meta-function. Note, that the @ref remove_const_t
270/// avoids the `::type` suffix and `typename` prefix when we want to use the
271/// result of the meta-function in templates.
272template <class t_TYPE>
273using remove_const_t = typename remove_const<t_TYPE>::type;
274#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
275
276
277} // close namespace bsl
278
279#endif
280
281// ----------------------------------------------------------------------------
282// Copyright 2013 Bloomberg Finance L.P.
283//
284// Licensed under the Apache License, Version 2.0 (the "License");
285// you may not use this file except in compliance with the License.
286// You may obtain a copy of the License at
287//
288// http://www.apache.org/licenses/LICENSE-2.0
289//
290// Unless required by applicable law or agreed to in writing, software
291// distributed under the License is distributed on an "AS IS" BASIS,
292// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
293// See the License for the specific language governing permissions and
294// limitations under the License.
295// ----------------------------- END-OF-FILE ----------------------------------
296
297/** @} */
298/** @} */
299/** @} */
#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_TYPE type
Definition bslmf_removeconst.h:181
Definition bslmf_removeconst.h:159
t_TYPE type
This typedef is an alias to the (template parameter) t_TYPE.
Definition bslmf_removeconst.h:164