BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmf_isarray.h
Go to the documentation of this file.
1/// @file bslmf_isarray.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_isarray.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ISARRAY
9#define INCLUDED_BSLMF_ISARRAY
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_isarray bslmf_isarray
15/// @brief Provide a compile-time check for array types.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_isarray
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_isarray-purpose"> Purpose</a>
25/// * <a href="#bslmf_isarray-classes"> Classes </a>
26/// * <a href="#bslmf_isarray-description"> Description </a>
27/// * <a href="#bslmf_isarray-usage"> Usage </a>
28/// * <a href="#bslmf_isarray-example-1-verify-array-types"> Example 1: Verify Array Types </a>
29///
30/// # Purpose {#bslmf_isarray-purpose}
31/// Provide a compile-time check for array types.
32///
33/// # Classes {#bslmf_isarray-classes}
34///
35/// - bsl::is_array: meta-function for detecting array types
36/// - bslmf::IsArray: non-standard meta-function for detecting array types
37/// - bsl::is_bounded_array: meta-function for detecting bounded array types
38/// - bsl::is_unbounded_array: meta-function for detecting unbounded array types
39/// - bsl::is_array_v: value of the `is_array` meta-function
40/// - bsl::is_bounded_array_v: value of the `is_bounded_array` meta-function
41/// - bsl::is_unbounded_array_v: value of the `is_unbounded_array` meta-function
42///
43/// # Description {#bslmf_isarray-description}
44/// This component defines four meta-functions, -- `bsl::is_array`,
45/// `bsl::is_bounded_array`, `bsl::is_unbounded_array`, and
46/// `BloombergLP::bslmf::IsArray` and three template variables, --
47/// `bsl::is_array_v`, `bsl::is_bounded_array_v`, and
48/// `bsl::is_unbounded_array_v` -- that contain the result values of the
49/// corresponding meta-functions. The `is_array` meta-functions may be used to
50/// query whether a type is an array type, while the `is_bounded_array`
51/// meta-functions may be used to query whether a type is an array of known
52/// bound, and the `is_unbounded_array` meta-functions may be used to query
53/// whether a type is an array of unknown bound.
54///
55/// `bsl::is_array` meets the requirements of the `is_array` template defined in
56/// the C++11 standard [meta.unary.cat], while `bslmf::IsArray` was devised
57/// before `is_array` was standardized. The two meta-functions are functionally
58/// equivalent. The major difference between them is that the result for
59/// `bsl::is_array` is indicated by the class member `value`, while the result
60/// for `bslmf::IsArray` is indicated by the class member `value`.
61///
62/// Note that `bsl::is_array` should be preferred over `bslmf::IsArray`, and in
63/// general, should be used by new components.
64///
65/// `bsl::is_bounded_array` and `bsl::is_bounded_array` meet the requirements of
66/// the `is_bounded_array` and `is_unbounded_array` template defined in the
67/// C++20 standard [meta.unary.prop].
68///
69/// Also note that the template variables `is_array_v`, `is_bounded_array_v`,
70/// and `is_unbounded_array_v` are defined in the standard as inline variables.
71/// If the current compiler supports the inline variable C++17 compiler feature,
72/// these variables are defined as `inline constexpr bool` variables.
73/// Otherwise, if the compiler supports the variable templates C++14 compiler
74/// feature, these variables are defined as non-inline `constexpr bool`
75/// variables. See `BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES` and
76/// `BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES` macros in the
77/// @ref bsls_compilerfeatures component for details.
78///
79/// ## Usage {#bslmf_isarray-usage}
80///
81///
82/// In this section we show intended use of this component.
83///
84/// ### Example 1: Verify Array Types {#bslmf_isarray-example-1-verify-array-types}
85///
86///
87/// Suppose that we want to assert whether a particular type is an array type.
88///
89/// First, we create three `typedef`s -- a non-array type, an array type with a
90/// known bound, and an array type and an array type with an unknown bound:
91/// @code
92/// typedef int MyNonArrayType;
93/// typedef int MySizedArrayType[12];
94/// typedef int MyUnsizedArrayType[];
95/// @endcode
96/// Now, we instantiate each of the the meta-functions for each of the
97/// `typedef`s and assert the `value` static data member of each instantiation:
98/// @code
99/// assert(false == bsl::is_array<MyType>::value);
100/// assert(true == bsl::is_array<MySizedArrayType>::value);
101/// assert(true == bsl::is_array<MyUnsizedArrayType>::value);
102///
103/// assert(false == bsl::is_bounded_array<MyType>::value);
104/// assert(true == bsl::is_bounded_array<MySizedArrayType>::value);
105/// assert(false == bsl::is_bounded_array<MyUnsizedArrayType>::value);
106///
107/// assert(false == bsl::is_unbounded_array<MyType>::value);
108/// assert(false == bsl::is_unbounded_array<MySizedArrayType>::value);
109/// assert(true == bsl::is_unbounded_array<MyUnsizedArrayType>::value);
110/// @endcode
111/// Note that if the current compiler supports the variable templates C++14
112/// feature then we can re-write the snippet of code above using the
113/// `bsl::is_array_v` variable as follows:
114/// @code
115/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
116/// assert(false == bsl::is_array_v<MyNonArrayType>);
117/// assert(true == bsl::is_array_v<MySizedArrayType>);
118/// assert(true == bsl::is_array_v<MyUnsizedArrayType>);
119///
120/// assert(false == bsl::is_bounded_array_v<MyNonArrayType>);
121/// assert(true == bsl::is_bounded_array_v<MySizedArrayType>);
122/// assert(false == bsl::is_bounded_array_v<MyUnsizedArrayType>);
123///
124/// assert(false == bsl::is_unbounded_array_v<MyNonArrayType>);
125/// assert(false == bsl::is_unbounded_array_v<MySizedArrayType>);
126/// assert(true == bsl::is_unbounded_array_v<MyUnsizedArrayType>);
127/// #endif
128/// @endcode
129/// @}
130/** @} */
131/** @} */
132
133/** @addtogroup bsl
134 * @{
135 */
136/** @addtogroup bslmf
137 * @{
138 */
139/** @addtogroup bslmf_isarray
140 * @{
141 */
142
143#include <bslscm_version.h>
144
146
148#include <bsls_keyword.h>
149
150#include <cstddef> // 'std::size_t'
151
152#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
153#include <cstdlib>
154#endif
155
156namespace bsl {
157
158 // ===============
159 // struct is_array
160 // ===============
161
162/// This `struct` template implements the `is_array` meta-function defined
163/// in the C++11 standard [meta.unary.cat] to determine if the (template
164/// parameter) `t_TYPE` is an array type. This `struct` derives from
165/// `bsl::true_type` if the `t_TYPE` is an array type, and `bsl::false_type`
166/// otherwise.
167template <class t_TYPE>
169};
170
171 // ========================================
172 // struct is_array<t_TYPE [t_NUM_ELEMENTS]>
173 // ========================================
174
175/// This specialization of `is_array`, for when the (template parameter)
176/// `t_TYPE` is an array type of known bound, derives from `bsl::true_type`.
177template <class t_TYPE, std::size_t t_NUM_ELEMENTS>
178struct is_array<t_TYPE[t_NUM_ELEMENTS]> : true_type {
179};
180
181 // ==========================
182 // struct is_array<t_TYPE []>
183 // ==========================
184
185/// This specialization of `is_array`, for when the (template parameter)
186/// `t_TYPE` is an array type of unknown bound, derives from
187/// `bsl::true_type`.
188template <class t_TYPE>
189struct is_array<t_TYPE[]> : true_type {
190};
191
192#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
193/// This template variable represents the result value of the
194/// `bsl::is_array` meta-function.
195template <class t_TYPE>
196BSLS_KEYWORD_INLINE_VARIABLE constexpr bool is_array_v =
198#endif
199
200 // =======================
201 // struct is_bounded_array
202 // =======================
203
204/// This `struct` template implements the `is_bounded_array` meta-function
205/// defined in the C++20 standard [meta.unary.cat] to determine if the
206/// (template parameter) `t_TYPE` is an array type of known bound. This
207/// `struct` derives from `bsl::true_type` if the `t_TYPE` is an array type
208/// of known bound, and `bsl::false_type` otherwise.
209template <class t_TYPE>
212
213/// This specialization of `is_bounded_array`, for when the (template
214/// parameter) `t_TYPE` is an array type of known bound, derives from
215/// `bsl::true_type`.
216template <class t_TYPE, std::size_t t_NUM_ELEMENTS>
217struct is_bounded_array<t_TYPE[t_NUM_ELEMENTS]> : true_type {
218};
219
220#ifdef BSLS_PLATFORM_CMP_IBM
221template <class t_TYPE>
222struct is_bounded_array<t_TYPE[]> : false_type {
223 // IBM miscomputes the SFINAE condition for arrays of unknown bound, so we
224 // provide an additional partial specialization for this platform.
225};
226#endif
227
228#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
229/// This template variable represents the result value of the
230/// `bsl::is_bounded_array` meta-function.
231template <class t_TYPE>
232BSLS_KEYWORD_INLINE_VARIABLE constexpr bool is_bounded_array_v =
234#endif
235
236 // =========================
237 // struct is_unbounded_array
238 // ==============--=========
239
240/// This `struct` template implements the `is_unbounded_array` meta-function
241/// defined in the C++20 standard [meta.unary.cat] to determine if the
242/// (template parameter) `t_TYPE` is an array type of unknown bound. This
243/// `struct` derives from `bsl::true_type` if the `t_TYPE` is an array type
244/// of unknown bound, and `bsl::false_type` otherwise.
245template <class t_TYPE>
248
249/// This specialization of `is_unbounded_array`, for when the (template
250/// parameter) `t_TYPE` is an array type of unknown bound, derives from
251/// `bsl::true_type`.
252template <class t_TYPE>
253struct is_unbounded_array<t_TYPE[]> : true_type {
254};
255
256#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
257/// This template variable represents the result value of the
258/// `bsl::is_unbounded_array_v` meta-function.
259template <class t_TYPE>
260BSLS_KEYWORD_INLINE_VARIABLE constexpr bool is_unbounded_array_v =
262#endif
263
264} // close namespace bsl
265
266
267namespace bslmf {
268
269 // ==============
270 // struct IsArray
271 // ==============
272
273/// This `struct` template implements a meta-function to determine if the
274/// (template parameter) `t_TYPE` is an array type. This `struct` derives
275/// from `bsl::true_type` if the `t_TYPE` is an array type, and
276/// `bsl::false_type` otherwise.
277///
278/// Note that although this `struct` is functionally equivalent to
279/// `bsl::is_array`, the use of `bsl::is_array` should be preferred.
280template <class t_TYPE>
281struct IsArray : bsl::is_array<t_TYPE>::type {
282};
283
284} // close package namespace
285
286
287#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
288// ============================================================================
289// BACKWARD COMPATIBILITY
290// ============================================================================
291
292#ifdef bslmf_IsArray
293#undef bslmf_IsArray
294#endif
295/// This alias is defined for backward compatibility.
296#define bslmf_IsArray bslmf::IsArray
297#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
298
299#endif
300
301// ----------------------------------------------------------------------------
302// Copyright 2013 Bloomberg Finance L.P.
303//
304// Licensed under the Apache License, Version 2.0 (the "License");
305// you may not use this file except in compliance with the License.
306// You may obtain a copy of the License at
307//
308// http://www.apache.org/licenses/LICENSE-2.0
309//
310// Unless required by applicable law or agreed to in writing, software
311// distributed under the License is distributed on an "AS IS" BASIS,
312// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
313// See the License for the specific language governing permissions and
314// limitations under the License.
315// ----------------------------- END-OF-FILE ----------------------------------
316
317/** @} */
318/** @} */
319/** @} */
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
Definition bdlbb_blob.h:576
Definition bslmf_integralconstant.h:244
Definition bslmf_isarray.h:168
Definition bslmf_isarray.h:210
Definition bslmf_isarray.h:246
Definition bslmf_isarray.h:281