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