Quick Links:

bal | bbl | bdl | bsl

Classes

Component bslma_isstdallocator
[Package bslma]

Provide a compile-time check for determining allocator types. More...

Classes

struct  bsl::IsStdAllocator< ALLOC, class, class >
struct  bsl::IsStdAllocator< std::allocator< TYPE > >
struct  bsl::IsStdAllocator< ::bsl::allocator< TYPE > >

Detailed Description

Outline
Purpose:
Provide a compile-time check for determining allocator types.
Classes:
bsl::IsStdAllocator standard meta-function for determining allocator types
bsl::IsStdAllocator_v the result of bsl::IsStdAllocator meta-function
Description:
This component defines a meta-function, bsl::IsStdAllocator and a template variable bsl::IsStdAllocator_v, that represents the result value of the bsl::IsStdAllocator meta-function.
bsl::IsStdAllocator is used to determine if a type meets the requirements for an allocator, as specified in [container.requirements.general]. Note that there is no is_allocator trait specified in the C++ standard, even though every implementation has one.
Two implementations are supplied; one for C++11 (and later) conforming compilers, and a pre-C++11 compatibility trait that gives different answers for custom allocator types due to the lack of decltype in the older language.
Also note that the template variable IsStdAllocator_v is defined in the style of the C++17 standard as an inline variable. If the current compiler supports the inline variable C++17 compiler feature, bsl::IsStdAllocator_v is defined as an inline constexpr bool variable. Otherwise, if the compiler supports the variable templates C++14 compiler feature, bsl::IsStdAllocator_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 if a class meets the requirements for an allocator.:
Suppose that we want to assert whether a set of types meet the requirements for allocators.
First, we create a struct type MyAllocator:
  struct MyAllocator
  {
      typedef int value_type;
      int *allocate(size_t count);
          // Allocate some memory for use by the caller.
  };
Now, we instantiate the bsl::IsStdAllocator template for both a type that does not meet the allocator requirements and the defined type MyClass, that does, asserting the value static data member of each instantiation.
  assert(false == bsl::IsStdAllocator<int>::value);
  #ifdef BSLS_COMPILERFEATURES_SUPPORT_DECLTYPE
  assert(true  == bsl::IsStdAllocator<MyAllocator>::value);
  #else
  assert(false  == bsl::IsStdAllocator<MyAllocator>::value);
  #endif
Note that if the current compiler supports the variable the templates C++14 feature then we can re-write the snippet of code above using the bsl::IsStdAllocator_v variable as follows:
#ifdef BSLS_COMPILERFEATURES_SUPPORT_VARIABLE_TEMPLATES
  assert(false == bsl::IsStdAllocator_v<int>);
  assert(true  == bsl::IsStdAllocator_v<MyAllocator>);
#endif