Quick Links: |
Provide a compile-time check for trivially default-constructible. More...
bsl::is_trivially_default_constructible | trait meta-function |
bsl::is_trivially_default_constructible_v | the result value |
bsl::is_trivially_default_constructible
and a template variable bsl::is_trivially_default_constructible_v
, that represents the result value of the bsl::is_trivially_default_constructible
meta-function, that may be used to query whether a type has a trivial default constructor as defined in section 12.1.5 of the C++11 standard [class.ctor]. bsl::is_trivially_default_constructible
has the same syntax as the is_trivially_default_constructible
template from the C++11 standard [meta.unary.prop]. However, unlike the template defined in the C++11 standard, which can determine the correct value for all types without requiring specialization, bsl::is_trivially_default_constructible
can, by default, determine the value for the following type categories only: Type Category Has Trivial Default Constructor ------------------- ------------------------------- reference types false fundamental types true enums true pointers true pointers to members true
bsl::is_trivially_default_constructible
returns false
, unless the type is explicitly specified to be trivially default-constructible, which can be done in two ways: bsl::is_trivially_default_constructible
having the type as the template parameter that inherits directly from bsl::true_type
. BSLMF_NESTED_TRAIT_DECLARATION
macro to define bsl::is_trivially_default_constructible
as the trait in the class definition of the type. is_trivially_default_constructible_v
is defined in the C++17 standard as an inline variable. If the current compiler supports the inline variable C++17 compiler feature, bsl::is_trivially_default_constructible_v
is defined as an inline constexpr bool
variable. Otherwise, if the compiler supports the variable templates C++14 compiler feature, bsl::is_trivially_default_constructible_v
is defined as a non-inline constexpr bool
variable. See BSLS_COMPILERFEATURES_SUPPORT_INLINE_VARIABLES
and BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
macros in bsls_compilerfeatures component for details. typedef int MyFundamentalType; typedef int& MyFundamentalTypeReference; class MyTriviallyDefaultConstructibleType { }; struct MyNonTriviallyDefaultConstructibleType { int d_data; MyNonTriviallyDefaultConstructibleType() : d_data(1) { } };
is_trivially_default_constructible
, we define a template specialization to specify that MyTriviallyDefaultConstructibleType
is trivially default-constructible: namespace bsl { template <> struct is_trivially_default_constructible< MyTriviallyDefaultConstructibleType> : bsl::true_type { // This template specialization for // 'is_trivially_default_constructible' indicates that // 'MyTriviallyDefaultConstructibleType' is a trivially // default-constructible type. }; } // close namespace bsl
bsl::is_trivially_default_constructible
: assert(true == bsl::is_trivially_default_constructible<MyFundamentalType>::value); assert(false == bsl::is_trivially_default_constructible< MyFundamentalTypeReference>::value); assert(true == bsl::is_trivially_default_constructible< MyTriviallyDefaultConstructibleType>::value); assert(false == bsl::is_trivially_default_constructible< MyNonTriviallyDefaultConstructibleType>::value);
#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES assert(true == bsl::is_trivially_default_constructible_v<MyFundamentalType>); assert(false == bsl::is_trivially_default_constructible_v<MyFundamentalTypeReference>); assert(true == bsl::is_trivially_default_constructible_v< MyTriviallyDefaultConstructibleType>); assert(false == bsl::is_trivially_default_constructible_v< MyNonTriviallyDefaultConstructibleType>); #endif