BDE 4.14.0 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
150/// `const`-qualifier has been removed. Note that this generic default
151/// template provides a `type` that is an alias to `t_TYPE` for when
152/// `t_TYPE` is not `const`-qualified. A template specialization is
153/// provided (below) that removes the `const`-qualifier for when `t_TYPE` is
154/// `const`-qualified.
155template <class t_TYPE>
157
158 // PUBLIC TYPES
159
160 /// This `typedef` is an alias to the (template parameter) `t_TYPE`.
161 typedef t_TYPE type;
162};
163
164 // =================================
165 // struct remove_const<t_TYPE const>
166 // =================================
167
168/// This partial specialization of `bsl::remove_const`, for when the
169/// (template parameter) `t_TYPE` is `const`-qualified, provides a
170/// `typedef`, `type`, that has the `const`-qualifier removed.
171template <class t_TYPE>
172struct remove_const<t_TYPE const> {
173
174 // PUBLIC TYPES
175
176 /// This `typedef` is an alias to the same type as the (template
177 /// parameter) `t_TYPE` except with the `const`-qualifier removed.
178 typedef t_TYPE type;
179};
180
181#if defined(BSLS_REMOVECONST_WORKAROUND_CONST_MULTIDIMENSIONAL_ARRAY)
182template <class t_TYPE, size_t N>
183struct remove_const<t_TYPE[N]> {
184 // This partial specialization of 'bsl::remove_const', for when the
185 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
186 // necessary to separately @ref remove_const on the element type, and then
187 // reconstruct the array dimensions.
188
189 // PUBLIC TYPES
190 typedef typename remove_const<t_TYPE>::type type[N];
191 // This 'typedef' is an alias to the same type as the (template
192 // parameter) 't_TYPE[N]' except with the 'const'-qualifier removed.
193};
194
195template <class t_TYPE, size_t N>
196struct remove_const<const t_TYPE[N]> {
197 // This partial specialization of 'bsl::remove_const', for when the
198 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
199 // necessary to separately @ref remove_const on the element type, and then
200 // reconstruct the array dimensions.
201
202 // PUBLIC TYPES
203 typedef typename remove_const<const t_TYPE>::type type[N];
204 // This 'typedef' is an alias to the same type as the (template
205 // parameter) 't_TYPE[N]' except with the 'const'-qualifier removed.
206};
207
208template <class t_TYPE>
209struct remove_const<t_TYPE[]> {
210 // This partial specialization of 'bsl::remove_const', for when the
211 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
212 // necessary to separately @ref remove_const on the element type, and then
213 // reconstruct the array dimensions.
214
215 // PUBLIC TYPES
216 typedef typename remove_const<t_TYPE>::type type[];
217 // This 'typedef' is an alias to the same type as the (template
218 // parameter) 't_TYPE[]' except with the 'const'-qualifier removed.
219};
220
221template <class t_TYPE>
222struct remove_const<const t_TYPE[]> {
223 // This partial specialization of 'bsl::remove_const', for when the
224 // (template parameter) 't_TYPE' is an array type. On IBM compilers, it is
225 // necessary to separately @ref remove_const on the element type, and then
226 // reconstruct the array dimensions.
227
228 // PUBLIC TYPES
229 typedef typename remove_const<const t_TYPE>::type type[];
230 // This 'typedef' is an alias to the same type as the (template
231 // parameter) 't_TYPE[]' except with the 'const'-qualifier removed.
232};
233#elif defined(BSLS_REMOVECONST_WORKAROUND_CONST_ARRAY)
234template <class t_TYPE>
235struct remove_const<t_TYPE[]> {
236 // This partial specialization of 'bsl::remove_const', for when the
237 // (template parameter) 't_TYPE' is an array type. On Microsoft compilers,
238 // it is necessary to separately @ref remove_const on the element type, and
239 // then reconstruct the array dimensions.
240
241 // PUBLIC TYPES
242 typedef typename remove_const<t_TYPE>::type type[];
243 // This 'typedef' is an alias to the same type as the (template
244 // parameter) 't_TYPE[]' except with the 'const'-qualifier removed.
245};
246
247template <class t_TYPE, size_t LENGTH>
248struct remove_const<t_TYPE[LENGTH]> {
249 // This partial specialization of 'bsl::remove_const', for when the
250 // (template parameter) 't_TYPE' is an array type. On Microsoft compilers,
251 // it is necessary to separately @ref remove_const on the element type, and
252 // then reconstruct the array dimensions.
253
254 // PUBLIC TYPES
255 typedef typename remove_const<t_TYPE>::type type[LENGTH];
256 // This 'typedef' is an alias to the same type as the (template
257 // parameter) 't_TYPE[N]' except with the 'const'-qualifier removed.
258};
259#endif
260
261#ifdef BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
262
263// ALIASES
264
265/// `remove_const_t` is an alias to the return type of the
266/// `bsl::remove_const` meta-function. Note, that the `remove_const_t`
267/// avoids the `::type` suffix and `typename` prefix when we want to use the
268/// result of the meta-function in templates.
269template <class t_TYPE>
270using remove_const_t = typename remove_const<t_TYPE>::type;
271#endif // BSLS_COMPILERFEATURES_SUPPORT_ALIAS_TEMPLATES
272
273
274} // close namespace bsl
275
276#endif
277
278// ----------------------------------------------------------------------------
279// Copyright 2013 Bloomberg Finance L.P.
280//
281// Licensed under the Apache License, Version 2.0 (the "License");
282// you may not use this file except in compliance with the License.
283// You may obtain a copy of the License at
284//
285// http://www.apache.org/licenses/LICENSE-2.0
286//
287// Unless required by applicable law or agreed to in writing, software
288// distributed under the License is distributed on an "AS IS" BASIS,
289// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
290// See the License for the specific language governing permissions and
291// limitations under the License.
292// ----------------------------- END-OF-FILE ----------------------------------
293
294/** @} */
295/** @} */
296/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_printmethods.h:283
t_TYPE type
Definition bslmf_removeconst.h:178
Definition bslmf_removeconst.h:156
t_TYPE type
This typedef is an alias to the (template parameter) t_TYPE.
Definition bslmf_removeconst.h:161