Quick Links:

bal | bbl | bdl | bsl

Classes

Component bslmf_istriviallycopyable
[Package bslmf]

Provide a meta-function for determining trivially copyable types. More...

Classes

struct  bslmf::IsTriviallyCopyable_DetectTrait< t_TYPE, t_K_INTRINSIC >
struct  bslmf::IsTriviallyCopyable_DetectTrait< t_TYPE, true >
struct  bslmf::IsTriviallyCopyable_Intrinsic< t_TYPE >
struct  bslmf::IsTriviallyCopyable_Intrinsic< void >
struct  bsl::is_trivially_copyable< t_TYPE & >
struct  bsl::is_trivially_copyable< const t_TYPE >
struct  bsl::is_trivially_copyable< volatile t_TYPE >
struct  bsl::is_trivially_copyable< const volatile t_TYPE >
struct  bsl::is_trivially_copyable< t_TYPE[t_LEN]>
struct  bsl::is_trivially_copyable< const t_TYPE[t_LEN]>
struct  bsl::is_trivially_copyable< volatile t_TYPE[t_LEN]>
struct  bsl::is_trivially_copyable< const volatile t_TYPE[t_LEN]>
struct  bsl::is_trivially_copyable< t_TYPE[]>
struct  bsl::is_trivially_copyable< const t_TYPE[]>
struct  bsl::is_trivially_copyable< volatile t_TYPE[]>
struct  bsl::is_trivially_copyable< const volatile t_TYPE[]>
struct  bsl::is_trivially_copyable< BloombergLP::bsls::TimeInterval >
struct  bsl::is_trivially_copyable< BloombergLP::bslmf::Nil >

Detailed Description

Outline
Purpose:
Provide a meta-function for determining trivially copyable types.
Classes:
bsl::is_trivially_copyable type-traits meta-function
bsl::is_trivially_copyable_v the result value of the meta-function
See also:
Component bslmf_integralconstant, Component bslmf_nestedtraitdeclaration
Description:
This component defines a meta-function, bsl::is_trivially_copyable and a template variable bsl::is_trivially_copyable_v, that represents the result value of the bsl::is_trivially_copyable meta-function, that may be used to query whether a type is trivially copyable as defined in section 3.9.3 of the C++11 standard [basic.types].
bsl::is_trivially_copyable has the same syntax as the is_trivially_copyable 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_copyable can, by default, determine the value for the following type categories only:
  Type Category        Is Trivially Copyable
  -------------        ---------------------
  reference types      false
  fundamental types    true
  enumerated types     true
  pointers             true
  pointers to members  true
For all other types, bsl::is_trivially_copyable returns false, unless the type is explicitly specified to be trivially copyable. This can be done in 2 ways:
  1. Define a template specialization for bsl::is_trivially_copyable having the type as the template parameter that inherits directly from bsl::true_type.
  2. Use the BSLMF_NESTED_TRAIT_DECLARATION macro to define bsl::is_trivially_copyable as a trait in the class definition of the type.
Note that the template variable is_trivially_copyable_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_copyable_v is defined as an inline constexpr bool variable. Otherwise, if the compiler supports the variable templates C++14 compiler feature, bsl::is_trivially_copyable_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.
Usage:
In this section we show intended use of this component.
Example 1: Verify Whether Types are Trivially Copyable:
Suppose that we want to assert whether a type is trivially copyable.
First, we define a set of types to evaluate:
  typedef int  MyFundamentalType;
  typedef int& MyFundamentalTypeReference;

  class MyTriviallyCopyableType {
  };

  struct MyNonTriviallyCopyableType {
      //...
  };
Then, since user-defined types cannot be automatically evaluated by is_trivially_copyable, we define a template specialization to specify that MyTriviallyCopyableType is trivially copyable:
  namespace bsl {

  template <>
  struct is_trivially_copyable<MyTriviallyCopyableType> : bsl::true_type {
      // This template specialization for 'is_trivially_copyable' indicates
      // that 'MyTriviallyCopyableType' is a trivially copyable type.
  };

  }  // close namespace bsl
Now, we verify whether each type is trivially copyable using bsl::is_trivially_copyable:
  assert(true  == bsl::is_trivially_copyable<MyFundamentalType>::value);
  assert(false == bsl::is_trivially_copyable<
                                         MyFundamentalTypeReference>::value);
  assert(true  == bsl::is_trivially_copyable<
                                            MyTriviallyCopyableType>::value);
  assert(false == bsl::is_trivially_copyable<
                                         MyNonTriviallyCopyableType>::value);
Note that if the current compiler supports the variable templates C++14 feature, then we can re-write the snippet of code above as follows:
  #ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
  assert(true  == bsl::is_trivially_copyable_v<MyFundamentalType>);
  assert(false == bsl::is_trivially_copyable_v<MyFundamentalTypeReference>);
  assert(true  == bsl::is_trivially_copyable_v<MyTriviallyCopyableType>);
  assert(false == bsl::is_trivially_copyable_v<MyNonTriviallyCopyableType>);
  #endif