BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslmf_istriviallydefaultconstructible.h
Go to the documentation of this file.
1/// @file bslmf_istriviallydefaultconstructible.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslmf_istriviallydefaultconstructible.h -*-C++-*-
8#ifndef INCLUDED_BSLMF_ISTRIVIALLYDEFAULTCONSTRUCTIBLE
9#define INCLUDED_BSLMF_ISTRIVIALLYDEFAULTCONSTRUCTIBLE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslmf_istriviallydefaultconstructible bslmf_istriviallydefaultconstructible
15/// @brief Provide a compile-time check for trivially default-constructible.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslmf
19/// @{
20/// @addtogroup bslmf_istriviallydefaultconstructible
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslmf_istriviallydefaultconstructible-purpose"> Purpose</a>
25/// * <a href="#bslmf_istriviallydefaultconstructible-classes"> Classes </a>
26/// * <a href="#bslmf_istriviallydefaultconstructible-description"> Description </a>
27/// * <a href="#bslmf_istriviallydefaultconstructible-usage"> Usage </a>
28/// * <a href="#bslmf_istriviallydefaultconstructible-example-1-verify-whether-types-are-trivially-default-constructible"> Example 1: Verify Whether Types are Trivially Default-Constructible </a>
29///
30/// # Purpose {#bslmf_istriviallydefaultconstructible-purpose}
31/// Provide a compile-time check for trivially default-constructible.
32///
33/// # Classes {#bslmf_istriviallydefaultconstructible-classes}
34///
35/// - bsl::is_trivially_default_constructible: trait meta-function
36/// - bsl::is_trivially_default_constructible_v: the result value
37///
38/// @see bslmf_integerconstant, bslmf_nestedtraitdeclaration
39///
40/// # Description {#bslmf_istriviallydefaultconstructible-description}
41/// This component defines a meta-function,
42/// `bsl::is_trivially_default_constructible` and a template variable
43/// `bsl::is_trivially_default_constructible_v`, that represents the result
44/// value of the `bsl::is_trivially_default_constructible` meta-function, that
45/// may be used to query whether a type has a trivial default constructor as
46/// defined in section 12.1.5 of the C++11 standard [class.ctor].
47///
48/// `bsl::is_trivially_default_constructible` has the same syntax as the
49/// `is_trivially_default_constructible` template from the C++11 standard
50/// [meta.unary.prop] and in C++11 or later build modes it delegates to the
51/// `std::is_trivially_default_constructible` which may use compiler intrinsics
52/// and otherwise depend on the compiler to determine the proper value. In
53/// C++03, unlike the template defined in the C++11 standard, which can
54/// determine the correct value for all types without requiring specialization,
55/// `bsl::is_trivially_default_constructible` can, by default, determine the
56/// value for the following type categories only:
57/// @code
58/// Type Category Has Trivial Default Constructor
59/// ------------------- -------------------------------
60/// reference types false
61/// fundamental types true
62/// `enum`s true
63/// pointers true
64/// pointers to members true
65/// @endcode
66/// In C++03, `bsl::is_trivially_default_constructible` returns `false` for all
67/// other types, unless the type is explicitly specified to be trivially
68/// default-constructible, which can be done in two ways:
69///
70/// 1. Define a template specialization for
71/// `bsl::is_trivially_default_constructible` having the type as the template
72/// parameter that inherits directly from `bsl::true_type`.
73/// 2. Use the `BSLMF_NESTED_TRAIT_DECLARATION` macro to define
74/// `bsl::is_trivially_default_constructible` as the trait in the class
75/// definition of the type.
76///
77/// Note that the template variable `is_trivially_default_constructible_v` is
78/// defined in the C++17 standard as an inline variable. If the current
79/// compiler supports the inline variable C++17 compiler feature,
80/// `bsl::is_trivially_default_constructible_v` is defined as an
81/// `inline constexpr bool` variable. Otherwise, if the compiler supports the
82/// variable templates C++14 compiler feature,
83/// `bsl::is_trivially_default_constructible_v` is defined as a non-inline
84/// `constexpr bool` variable. See
85/// `BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES` and
86/// `BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES` macros in
87/// bsls_compilerfeatures component for details.
88///
89/// ## Usage {#bslmf_istriviallydefaultconstructible-usage}
90///
91///
92/// In this section we show intended use of this component.
93///
94/// ### Example 1: Verify Whether Types are Trivially Default-Constructible {#bslmf_istriviallydefaultconstructible-example-1-verify-whether-types-are-trivially-default-constructible}
95///
96///
97/// Suppose that we want to assert whether a type is trivially
98/// default-constructible.
99///
100/// First, we define a set of types to evaluate:
101/// @code
102/// typedef int MyFundamentalType;
103/// typedef int& MyFundamentalTypeReference;
104///
105/// class MyTriviallyDefaultConstructibleType {
106/// };
107///
108/// struct MyNonTriviallyDefaultConstructibleType {
109///
110/// int d_data;
111///
112/// MyNonTriviallyDefaultConstructibleType()
113/// : d_data(1)
114/// {
115/// }
116/// };
117/// @endcode
118/// Then, since user-defined types cannot be automatically evaluated by
119/// `is_trivially_default_constructible`, we define a template specialization to
120/// specify that `MyTriviallyDefaultConstructibleType` is trivially
121/// default-constructible:
122/// @code
123/// namespace bsl {
124///
125/// template <>
126/// struct is_trivially_default_constructible<
127/// MyTriviallyDefaultConstructibleType> : bsl::true_type {
128/// // This template specialization for
129/// // 'is_trivially_default_constructible' indicates that
130/// // 'MyTriviallyDefaultConstructibleType' is a trivially
131/// // default-constructible type.
132/// };
133///
134/// } // close namespace bsl
135/// @endcode
136/// Now, we verify whether each type is trivially default-constructible using
137/// `bsl::is_trivially_default_constructible`:
138/// @code
139/// assert(true ==
140/// bsl::is_trivially_default_constructible<MyFundamentalType>::value);
141/// assert(false ==
142/// bsl::is_trivially_default_constructible<
143/// MyFundamentalTypeReference>::value);
144/// assert(true ==
145/// bsl::is_trivially_default_constructible<
146/// MyTriviallyDefaultConstructibleType>::value);
147/// assert(false ==
148/// bsl::is_trivially_default_constructible<
149/// MyNonTriviallyDefaultConstructibleType>::value);
150/// @endcode
151/// Note that if the current compiler supports the variable templates C++14
152/// feature, then we can re-write the snippet of code above as follows:
153/// @code
154/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
155/// assert(true ==
156/// bsl::is_trivially_default_constructible_v<MyFundamentalType>);
157/// assert(false ==
158/// bsl::is_trivially_default_constructible_v<MyFundamentalTypeReference>);
159/// assert(true ==
160/// bsl::is_trivially_default_constructible_v<
161/// MyTriviallyDefaultConstructibleType>);
162/// assert(false ==
163/// bsl::is_trivially_default_constructible_v<
164/// MyNonTriviallyDefaultConstructibleType>);
165/// #endif
166/// @endcode
167/// @}
168/** @} */
169/** @} */
170
171/** @addtogroup bsl
172 * @{
173 */
174/** @addtogroup bslmf
175 * @{
176 */
177/** @addtogroup bslmf_istriviallydefaultconstructible
178 * @{
179 */
180
181#include <bslscm_version.h>
182
185#include <bslmf_isenum.h>
186#include <bslmf_isfundamental.h>
188#include <bslmf_ispointer.h>
189#include <bslmf_isreference.h>
190
192#include <bsls_keyword.h>
193#include <bsls_platform.h>
194
195#include <stddef.h>
196
197#ifdef BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
198# include <type_traits>
199#endif
200
201namespace bsl {
202
203template <class t_TYPE>
204struct is_trivially_default_constructible;
205
206} // close namespace bsl
207
208
209namespace bslmf {
210
211 // ==================================================
212 // struct IsTriviallyDefaultConstructible_DetectTrait
213 // ==================================================
214
215/// This `struct` template implements a meta-function to determine whether
216/// the (non-cv-qualified) (template parameter) `t_TYPE` has been explicitly
217/// tagged with the trivially default constructible trait. If the flag
218/// `t_K_INTRINSIC` is `true` then the compiler has already determined that
219/// `t_TYPE` is trivially default constructible without user intervention,
220/// and the check for nested traits can be optimized away.
221template <class t_TYPE, bool t_K_INTRINSIC = false>
223: DetectNestedTrait<t_TYPE, bsl::is_trivially_default_constructible>::type {
224};
225
226/// This `struct` template implements a meta-function to determine whether
227/// the (non-cv-qualified) (template parameter) `t_TYPE` is trivially
228/// default constructible.
229template <class t_TYPE>
233
234 // ==========================================
235 // struct IsTriviallyDefaultConstructible_Imp
236 // ==========================================
237
238#ifdef BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
239/// This `struct` template implements a meta-function to determine whether
240/// the (non-cv-qualified) (template parameter) `t_TYPE` is trivially
241/// default constructible.
242template <class t_TYPE>
245 t_TYPE,
246 ::std::is_trivially_default_constructible<t_TYPE>::value>::type {
247};
248#else
249/// This `struct` template implements a meta-function to determine whether
250/// the (non-cv-qualified) (template parameter) `t_TYPE` is trivially
251/// default-constructible.
252template <class t_TYPE>
255 t_TYPE,
256 bsl::is_fundamental<t_TYPE>::value ||
257 bsl::is_enum<t_TYPE>::value ||
258 bsl::is_pointer<t_TYPE>::value ||
259 bsl::is_member_pointer<t_TYPE>::value>::type {
260};
261
262
263/// This explicit specialization reports that `void` is not a trivially
264/// default constructible type, despite being a fundamental type.
265template <>
268#endif
269
270
271} // close package namespace
272
273
274namespace bsl {
275
276 // =========================================
277 // struct is_trivially_default_constructible
278 // =========================================
279
280/// This `struct` template implements a meta-function to determine whether
281/// the (template parameter) `t_TYPE` is trivially default-constructible.
282/// This `struct` derives from `bsl::true_type` if the `t_TYPE` is trivially
283/// default-constructible, and from `bsl::false_type` otherwise. This
284/// meta-function has the same syntax as the
285/// `is_trivially_default_constructible` meta-function defined in the C++11
286/// standard [meta.unary.prop]; however, this meta-function can
287/// automatically determine the value for the following types only:
288/// reference types, fundamental types, enums, pointers to members, and
289/// types declared to have the `bsl::is_trivially_default_constructible`
290/// trait using the `BSLMF_NESTED_TRAIT_DECLARATION` macro (and the value
291/// for other types defaults to `false`). To support other trivially
292/// default-constructible types, this template must be specialized to
293/// inherit from `bsl::true_type` for them.
294template <class t_TYPE>
296: BloombergLP::bslmf::IsTriviallyDefaultConstructible_Imp<t_TYPE>::type {
297};
298
299/// This partial specialization ensures that const-qualified types have the
300/// same result as their element type.
301template <class t_TYPE>
304};
305
306/// This partial specialization ensures that volatile-qualified types have
307/// the same result as their element type.
308template <class t_TYPE>
311};
312
313/// This partial specialization ensures that const-volatile-qualified types
314/// have the same result as their element type.
315template <class t_TYPE>
316struct is_trivially_default_constructible<const volatile t_TYPE>
318};
319
320/// This partial specialization ensures that array types have the same
321/// result as their element type.
322template <class t_TYPE, size_t t_LEN>
325};
326
327/// This partial specialization ensures that const-qualified array types
328/// have the same result as their element type.
329template <class t_TYPE, size_t t_LEN>
330struct is_trivially_default_constructible<const t_TYPE[t_LEN]>
332};
333
334/// This partial specialization ensures that volatile-qualified array types
335/// have the same result as their element type.
336template <class t_TYPE, size_t t_LEN>
337struct is_trivially_default_constructible<volatile t_TYPE[t_LEN]>
339};
340
341/// This partial specialization ensures that const-volatile-qualified array
342/// types have the same result as their element type.
343template <class t_TYPE, size_t t_LEN>
344struct is_trivially_default_constructible<const volatile t_TYPE[t_LEN]>
346};
347
348#if !defined(BSLS_PLATFORM_CMP_IBM)
349// Last checked with the xlC 12.1 compiler. The IBM xlC compiler has problems
350// correctly handling arrays of unknown bound as template parameters.
351
352/// This partial specialization ensures that array-of-unknown-bound types
353/// have the same result as their element type.
354template <class t_TYPE>
358
359/// This partial specialization ensures that const-qualified
360/// array-of-unknown-bound types have the same result as their element type.
361template <class t_TYPE>
364};
365
366/// This partial specialization ensures that volatile-qualified
367/// array-of-unknown-bound types have the same result as their element type.
368template <class t_TYPE>
369struct is_trivially_default_constructible<volatile t_TYPE[]>
371};
372
373/// This partial specialization ensures that const-volatile-qualified
374/// array-of-unknown-bound types have the same result as their element type.
375template <class t_TYPE>
376struct is_trivially_default_constructible<const volatile t_TYPE[]>
378};
379#endif
380
381#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
382/// This template variable represents the result value of the
383/// `bsl::is_trivially_default_constructible` meta-function.
384template <class t_TYPE>
385BSLS_KEYWORD_INLINE_VARIABLE constexpr bool
386 is_trivially_default_constructible_v =
388#endif
389
390} // close namespace bsl
391
392#endif
393
394// ----------------------------------------------------------------------------
395// Copyright 2013 Bloomberg Finance L.P.
396//
397// Licensed under the Apache License, Version 2.0 (the "License");
398// you may not use this file except in compliance with the License.
399// You may obtain a copy of the License at
400//
401// http://www.apache.org/licenses/LICENSE-2.0
402//
403// Unless required by applicable law or agreed to in writing, software
404// distributed under the License is distributed on an "AS IS" BASIS,
405// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
406// See the License for the specific language governing permissions and
407// limitations under the License.
408// ----------------------------- END-OF-FILE ----------------------------------
409
410/** @} */
411/** @} */
412/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_KEYWORD_INLINE_VARIABLE
Definition bsls_keyword.h:665
Definition bdlat_valuetypefunctions.h:939
Definition bdlbb_blob.h:579
Definition bslmf_istriviallydefaultconstructible.h:296
Definition bslmf_detectnestedtrait.h:467
Definition bslmf_istriviallydefaultconstructible.h:223
Definition bslmf_istriviallydefaultconstructible.h:259