Provide a compile-time check for determining allocator types.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide a compile-time check for determining allocator types.
-
- Classes:
-
-
- 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);
};
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. 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