BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_isconst.h
Go to the documentation of this file.
1/// @file bslmf_isconst.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_isconst.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ISCONST
9#define INCLUDED_BSLMF_ISCONST
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_isconst bslmf_isconst
15/// @brief Provide a compile-time check for `const`-qualified types.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_isconst
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_isconst-purpose"> Purpose</a>
25/// * <a href="#bslmf_isconst-classes"> Classes </a>
26/// * <a href="#bslmf_isconst-description"> Description </a>
27/// * <a href="#bslmf_isconst-usage"> Usage </a>
28/// * <a href="#bslmf_isconst-example-1-verify-const-types"> Example 1: Verify const Types </a>
29///
30/// # Purpose {#bslmf_isconst-purpose}
31/// Provide a compile-time check for `const`-qualified types.
32///
33/// # Classes {#bslmf_isconst-classes}
34///
35/// - bsl::is_const: meta-function for determining `const`-qualified types
36/// - bsl::is_const_v: the result value of the `bsl::is_const` meta-function
37///
38/// @see bslmf_integralconstant
39///
40/// # Description {#bslmf_isconst-description}
41/// This component defines a meta-function, `bsl::is_const` and a
42/// template variable `bsl::is_const_v`, that represents the result value of the
43/// `bsl::is_const` meta-function, that may be used to query whether a type is
44/// `const`-qualified as defined in the C++ standard [basic.type.qualifier].
45///
46/// `bsl::is_const` meets the requirements of the `is_const` template defined in
47/// the C++11 standard [meta.unary.prop].
48///
49/// Note that the template variable `is_const_v` is defined in the C++17
50/// standard as an inline variable. If the current compiler supports the inline
51/// variable C++17 compiler feature, `bsl::is_const_v` is defined as an
52/// `inline constexpr bool` variable. Otherwise, if the compiler supports the
53/// variable templates C++14 compiler feature, `bsl::is_const_v` is defined
54/// as a non-inline `constexpr bool` variable. See
55/// `BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES` and
56/// `BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES` macros in
57/// bsls_compilerfeatures component for details.
58///
59/// ## Usage {#bslmf_isconst-usage}
60///
61///
62/// In this section we show intended use of this component.
63///
64/// ### Example 1: Verify const Types {#bslmf_isconst-example-1-verify-const-types}
65///
66///
67/// Suppose that we want to assert whether a particular type is
68/// `const`-qualified.
69///
70/// First, we create two `typedef`s -- a `const`-qualified type and an
71/// unqualified type:
72/// @code
73/// typedef int MyType;
74/// typedef const int MyConstType;
75/// @endcode
76/// Now, we instantiate the `bsl::is_const` template for each of the `typedef`s
77/// and assert the `value` static data member of each instantiation:
78/// @code
79/// assert(false == bsl::is_const<MyType>::value);
80/// assert(true == bsl::is_const<MyConstType>::value);
81/// @endcode
82/// Note that if the current compiler supports the variable templates C++14
83/// feature then we can re-write the snippet of code above using the
84/// `bsl::is_const_v` variable as follows:
85/// @code
86/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
87/// assert(false == bsl::is_const_v<MyType>);
88/// assert(true == bsl::is_const_v<MyConstType>);
89/// #endif
90/// @endcode
91/// @}
92/** @} */
93/** @} */
94
95/** @addtogroup bsl
96 * @{
97 */
98/** @addtogroup bslmf
99 * @{
100 */
101/** @addtogroup bslmf_isconst
102 * @{
103 */
104
105#include <bslscm_version.h>
106
108#include <bslmf_issame.h>
109
111#include <bsls_keyword.h>
112#include <bsls_platform.h>
113
114#include <stddef.h>
115
116#if (defined(BSLS_PLATFORM_CMP_MSVC) && BSLS_PLATFORM_CMP_VERSION < 1910) \
117 || defined(BSLS_PLATFORM_CMP_IBM)
118// The Microsoft compiler does not recognize array-types as cv-qualified (when
119// the element type is cv-qualified) when performing matching for partial
120// template specialization, but does get the correct result when performing
121// overload resolution for functions (taking arrays by reference). Given the
122// function dispatch behavior being correct, we choose to work around this
123// compiler bug, rather than try to report compiler behavior, as the compiler
124// itself is inconsistent depending on how the trait might be used. This also
125// corresponds to how Microsft itself implements the trait in VC2010 and later.
126// Last tested against VC 2015 (Release Candidate).
127# define BSLMF_ISCONST_COMPILER_DOES_NOT_DETECT_CV_QUALIFIED_ARRAY_ELEMENT 1
128#endif
129
130namespace bsl {
131
132 // ===============
133 // struct is_const
134 // ===============
135
136/// This `struct` template implements the `is_const` meta-function defined
137/// in the C++11 standard [meta.unary.cat] to determine if the (template
138/// parameter) `t_TYPE` is `const`-qualified. This `struct` derives from
139/// `bsl::true_type` if the `t_TYPE` is `const`-qualified, and
140/// `bsl::false_type` otherwise. Note that this generic default template
141/// derives from `bsl::false_type`. A template specialization is provided
142/// (below) that derives from `bsl::true_type`.
143template <class t_TYPE>
145};
146
147 // =============================
148 // struct is_const<t_TYPE const>
149 // =============================
150
151#if defined(BSLS_PLATFORM_CMP_SUN) && BSLS_PLATFORM_CMP_VERSION < 0x5130
152template <class t_TYPE>
153struct is_const<const t_TYPE>
154: integral_constant<bool, !is_same<t_TYPE, const t_TYPE>::value> {
155 // This partial specialization of 'is_const', for when the (template
156 // parameter) 't_TYPE' is 'const'-qualified, derives from 'bsl::true_type'.
157 // Note that the Solaris CC compiler misdiagnoses cv-qualified "abominable"
158 // function types as being cv-qualified themselves. The correct result is
159 // obtained by delegating the result to a call through 'is_same'.
160};
161#else
162/// This partial specialization of `is_const`, for when the (template
163/// parameter) `t_TYPE` is `const`-qualified, derives from `bsl::true_type`.
164template <class t_TYPE>
165struct is_const<const t_TYPE> : true_type {
166};
167#endif
168
169
170#if defined(BSLMF_ISCONST_COMPILER_DOES_NOT_DETECT_CV_QUALIFIED_ARRAY_ELEMENT)
171// The Microsoft compiler does not recognize array-types as cv-qualified when
172// the element type is cv-qualified when performing matching for partial
173// template specialization, but does get the correct result when performing
174// overload resolution for functions (taking arrays by reference). Given the
175// function dispatch behavior being correct, we choose to work around this
176// compiler bug, rather than try to report compiler behavior, as the compiler
177// itself is inconsistent depeoning on how the trait might be used. This also
178// corresponds to how Microsft itself implements the trait in VC2010 and later.
179// Last tested against VC 2015 (Release Candidate).
180
181template <class t_TYPE>
182struct is_const<const t_TYPE[]> : true_type {
183 // This partial specialization of 'is_const', for when the (template
184 // parameter) 't_TYPE' is 'const'-qualified, derives from 'bsl::true_type'.
185 // Note that this single specialization is sufficient to work around the
186 // MSVC issue, even for multidimensional arrays.
187};
188
189template <class t_TYPE, size_t LENGTH>
190struct is_const<const t_TYPE[LENGTH]> : true_type {
191 // This partial specialization of 'is_const', for when the (template
192 // parameter) 't_TYPE' is 'const'-qualified, derives from 'bsl::true_type'.
193 // Note that this single specialization is sufficient to work around the
194 // MSVC issue, even for multidimensional arrays.
195};
196#endif
197
198#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
199/// This template variable represents the result value of the
200/// `bsl::is_const` meta-function.
201template <class t_TYPE>
202BSLS_KEYWORD_INLINE_VARIABLE constexpr bool is_const_v =
204#endif
205
206} // close namespace bsl
207
208#endif
209
210// ----------------------------------------------------------------------------
211// Copyright 2013 Bloomberg Finance L.P.
212//
213// Licensed under the Apache License, Version 2.0 (the "License");
214// you may not use this file except in compliance with the License.
215// You may obtain a copy of the License at
216//
217// http://www.apache.org/licenses/LICENSE-2.0
218//
219// Unless required by applicable law or agreed to in writing, software
220// distributed under the License is distributed on an "AS IS" BASIS,
221// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
222// See the License for the specific language governing permissions and
223// limitations under the License.
224// ----------------------------- END-OF-FILE ----------------------------------
225
226/** @} */
227/** @} */
228/** @} */
static const t_TYPE value
Definition bslmf_integralconstant.h:258
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_INLINE_VARIABLE
Definition bsls_keyword.h:623
Definition bdlb_printmethods.h:283
integral_constant< bool, true > true_type
Definition bslmf_integralconstant.h:303
Definition bslmf_integralconstant.h:244
Definition bslmf_isconst.h:144