BDE 4.14.0 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]. However, unlike the template defined in the C++11
51/// standard, which can determine the correct value for all types without
52/// requiring specialization, `bsl::is_trivially_default_constructible` can, by
53/// default, determine the value for the following type categories only:
54/// @code
55/// Type Category Has Trivial Default Constructor
56/// ------------------- -------------------------------
57/// reference types false
58/// fundamental types true
59/// enums true
60/// pointers true
61/// pointers to members true
62/// @endcode
63/// For all other types, `bsl::is_trivially_default_constructible` returns
64/// `false`, unless the type is explicitly specified to be trivially
65/// default-constructible, which can be done in two ways:
66///
67/// 1. Define a template specialization for
68/// `bsl::is_trivially_default_constructible` having the type as the template
69/// parameter that inherits directly from `bsl::true_type`.
70/// 2. Use the `BSLMF_NESTED_TRAIT_DECLARATION` macro to define
71/// `bsl::is_trivially_default_constructible` as the trait in the class
72/// definition of the type.
73///
74/// Note that the template variable `is_trivially_default_constructible_v` is
75/// defined in the C++17 standard as an inline variable. If the current
76/// compiler supports the inline variable C++17 compiler feature,
77/// `bsl::is_trivially_default_constructible_v` is defined as an
78/// `inline constexpr bool` variable. Otherwise, if the compiler supports the
79/// variable templates C++14 compiler feature,
80/// `bsl::is_trivially_default_constructible_v` is defined as a non-inline
81/// `constexpr bool` variable. See
82/// `BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES` and
83/// `BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES` macros in
84/// bsls_compilerfeatures component for details.
85///
86/// ## Usage {#bslmf_istriviallydefaultconstructible-usage}
87///
88///
89/// In this section we show intended use of this component.
90///
91/// ### Example 1: Verify Whether Types are Trivially Default-Constructible {#bslmf_istriviallydefaultconstructible-example-1-verify-whether-types-are-trivially-default-constructible}
92///
93///
94/// Suppose that we want to assert whether a type is trivially
95/// default-constructible.
96///
97/// First, we define a set of types to evaluate:
98/// @code
99/// typedef int MyFundamentalType;
100/// typedef int& MyFundamentalTypeReference;
101///
102/// class MyTriviallyDefaultConstructibleType {
103/// };
104///
105/// struct MyNonTriviallyDefaultConstructibleType {
106///
107/// int d_data;
108///
109/// MyNonTriviallyDefaultConstructibleType()
110/// : d_data(1)
111/// {
112/// }
113/// };
114/// @endcode
115/// Then, since user-defined types cannot be automatically evaluated by
116/// `is_trivially_default_constructible`, we define a template specialization to
117/// specify that `MyTriviallyDefaultConstructibleType` is trivially
118/// default-constructible:
119/// @code
120/// namespace bsl {
121///
122/// template <>
123/// struct is_trivially_default_constructible<
124/// MyTriviallyDefaultConstructibleType> : bsl::true_type {
125/// // This template specialization for
126/// // 'is_trivially_default_constructible' indicates that
127/// // 'MyTriviallyDefaultConstructibleType' is a trivially
128/// // default-constructible type.
129/// };
130///
131/// } // close namespace bsl
132/// @endcode
133/// Now, we verify whether each type is trivially default-constructible using
134/// `bsl::is_trivially_default_constructible`:
135/// @code
136/// assert(true ==
137/// bsl::is_trivially_default_constructible<MyFundamentalType>::value);
138/// assert(false ==
139/// bsl::is_trivially_default_constructible<
140/// MyFundamentalTypeReference>::value);
141/// assert(true ==
142/// bsl::is_trivially_default_constructible<
143/// MyTriviallyDefaultConstructibleType>::value);
144/// assert(false ==
145/// bsl::is_trivially_default_constructible<
146/// MyNonTriviallyDefaultConstructibleType>::value);
147/// @endcode
148/// Note that if the current compiler supports the variable templates C++14
149/// feature, then we can re-write the snippet of code above as follows:
150/// @code
151/// #ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
152/// assert(true ==
153/// bsl::is_trivially_default_constructible_v<MyFundamentalType>);
154/// assert(false ==
155/// bsl::is_trivially_default_constructible_v<MyFundamentalTypeReference>);
156/// assert(true ==
157/// bsl::is_trivially_default_constructible_v<
158/// MyTriviallyDefaultConstructibleType>);
159/// assert(false ==
160/// bsl::is_trivially_default_constructible_v<
161/// MyNonTriviallyDefaultConstructibleType>);
162/// #endif
163/// @endcode
164/// @}
165/** @} */
166/** @} */
167
168/** @addtogroup bsl
169 * @{
170 */
171/** @addtogroup bslmf
172 * @{
173 */
174/** @addtogroup bslmf_istriviallydefaultconstructible
175 * @{
176 */
177
178#include <bslscm_version.h>
179
182#include <bslmf_isenum.h>
183#include <bslmf_isfundamental.h>
185#include <bslmf_ispointer.h>
186#include <bslmf_isreference.h>
187
189#include <bsls_keyword.h>
190#include <bsls_platform.h>
191
192#include <stddef.h>
193
194#ifdef BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
195# include <type_traits>
196#endif
197
198namespace bsl {
199
200template <class t_TYPE>
201struct is_trivially_default_constructible;
202
203} // close namespace bsl
204
205
206namespace bslmf {
207
208 // ==================================================
209 // struct IsTriviallyDefaultConstructible_DetectTrait
210 // ==================================================
211
212/// This `struct` template implements a meta-function to determine whether
213/// the (non-cv-qualified) (template parameter) `t_TYPE` has been explicitly
214/// tagged with the trivially default constructible trait. If the flag
215/// `t_K_INTRINSIC` is `true` then the compiler has already determined that
216/// `t_TYPE` is trivially default constructible without user intervention,
217/// and the check for nested traits can be optimized away.
218template <class t_TYPE, bool t_K_INTRINSIC = false>
220: DetectNestedTrait<t_TYPE, bsl::is_trivially_default_constructible>::type {
221};
222
223/// This `struct` template implements a meta-function to determine whether
224/// the (non-cv-qualified) (template parameter) `t_TYPE` is trivially
225/// default constructible.
226template <class t_TYPE>
230
231 // ==========================================
232 // struct IsTriviallyDefaultConstructible_Imp
233 // ==========================================
234
235#ifdef BSLS_COMPILERFEATURES_SUPPORT_TRAITS_HEADER
236/// This `struct` template implements a meta-function to determine whether
237/// the (non-cv-qualified) (template parameter) `t_TYPE` is trivially
238/// default constructible.
239template <class t_TYPE>
242 t_TYPE,
243 ::std::is_trivially_default_constructible<t_TYPE>::value>::type {
244};
245#else
246/// This `struct` template implements a meta-function to determine whether
247/// the (non-cv-qualified) (template parameter) `t_TYPE` is trivially
248/// default-constructible.
249template <class t_TYPE>
252 t_TYPE,
253 bsl::is_fundamental<t_TYPE>::value ||
254 bsl::is_enum<t_TYPE>::value ||
255 bsl::is_pointer<t_TYPE>::value ||
256 bsl::is_member_pointer<t_TYPE>::value>::type {
257};
258
259
260/// This explicit specialization reports that `void` is not a trivially
261/// default constructible type, despite being a fundamental type.
262template <>
265#endif
266
267
268} // close package namespace
269
270
271namespace bsl {
272
273 // =========================================
274 // struct is_trivially_default_constructible
275 // =========================================
276
277/// This `struct` template implements a meta-function to determine whether
278/// the (template parameter) `t_TYPE` is trivially default-constructible.
279/// This `struct` derives from `bsl::true_type` if the `t_TYPE` is trivially
280/// default-constructible, and from `bsl::false_type` otherwise. This
281/// meta-function has the same syntax as the
282/// `is_trivially_default_constructible` meta-function defined in the C++11
283/// standard [meta.unary.prop]; however, this meta-function can
284/// automatically determine the value for the following types only:
285/// reference types, fundamental types, enums, pointers to members, and
286/// types declared to have the `bsl::is_trivially_default_constructible`
287/// trait using the `BSLMF_NESTED_TRAIT_DECLARATION` macro (and the value
288/// for other types defaults to `false`). To support other trivially
289/// default-constructible types, this template must be specialized to
290/// inherit from `bsl::true_type` for them.
291template <class t_TYPE>
293: BloombergLP::bslmf::IsTriviallyDefaultConstructible_Imp<t_TYPE>::type {
294};
295
296/// This partial specialization ensures that const-qualified types have the
297/// same result as their element type.
298template <class t_TYPE>
301};
302
303/// This partial specialization ensures that volatile-qualified types have
304/// the same result as their element type.
305template <class t_TYPE>
308};
309
310/// This partial specialization ensures that const-volatile-qualified types
311/// have the same result as their element type.
312template <class t_TYPE>
313struct is_trivially_default_constructible<const volatile t_TYPE>
315};
316
317/// This partial specialization ensures that array types have the same
318/// result as their element type.
319template <class t_TYPE, size_t t_LEN>
322};
323
324/// This partial specialization ensures that const-qualified array types
325/// have the same result as their element type.
326template <class t_TYPE, size_t t_LEN>
327struct is_trivially_default_constructible<const t_TYPE[t_LEN]>
329};
330
331/// This partial specialization ensures that volatile-qualified array types
332/// have the same result as their element type.
333template <class t_TYPE, size_t t_LEN>
334struct is_trivially_default_constructible<volatile t_TYPE[t_LEN]>
336};
337
338/// This partial specialization ensures that const-volatile-qualified array
339/// types have the same result as their element type.
340template <class t_TYPE, size_t t_LEN>
341struct is_trivially_default_constructible<const volatile t_TYPE[t_LEN]>
343};
344
345#if !defined(BSLS_PLATFORM_CMP_IBM)
346// Last checked with the xlC 12.1 compiler. The IBM xlC compiler has problems
347// correctly handling arrays of unknown bound as template parameters.
348
349/// This partial specialization ensures that array-of-unknown-bound types
350/// have the same result as their element type.
351template <class t_TYPE>
355
356/// This partial specialization ensures that const-qualified
357/// array-of-unknown-bound types have the same result as their element type.
358template <class t_TYPE>
361};
362
363/// This partial specialization ensures that volatile-qualified
364/// array-of-unknown-bound types have the same result as their element type.
365template <class t_TYPE>
366struct is_trivially_default_constructible<volatile t_TYPE[]>
368};
369
370/// This partial specialization ensures that const-volatile-qualified
371/// array-of-unknown-bound types have the same result as their element type.
372template <class t_TYPE>
373struct is_trivially_default_constructible<const volatile t_TYPE[]>
375};
376#endif
377
378#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
379/// This template variable represents the result value of the
380/// `bsl::is_trivially_default_constructible` meta-function.
381template <class t_TYPE>
382BSLS_KEYWORD_INLINE_VARIABLE constexpr bool
383 is_trivially_default_constructible_v =
385#endif
386
387} // close namespace bsl
388
389#endif
390
391// ----------------------------------------------------------------------------
392// Copyright 2013 Bloomberg Finance L.P.
393//
394// Licensed under the Apache License, Version 2.0 (the "License");
395// you may not use this file except in compliance with the License.
396// You may obtain a copy of the License at
397//
398// http://www.apache.org/licenses/LICENSE-2.0
399//
400// Unless required by applicable law or agreed to in writing, software
401// distributed under the License is distributed on an "AS IS" BASIS,
402// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
403// See the License for the specific language governing permissions and
404// limitations under the License.
405// ----------------------------- END-OF-FILE ----------------------------------
406
407/** @} */
408/** @} */
409/** @} */
#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_istriviallydefaultconstructible.h:293
Definition bslmf_detectnestedtrait.h:464
Definition bslmf_istriviallydefaultconstructible.h:220
Definition bslmf_istriviallydefaultconstructible.h:256