BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsltf_stdalloctesttype

Detailed Description

Outline

Purpose

Provide a class for testing that allocates with standard allocator

Classes

See also
bsltf_templatetestfacility

Description

This component provides a single, unconstrained (value-semantic) attribute class, StdAllocTestType, that takes a standard allocator type as a template argument and uses an object of that type to allocate memory. Note that this class does NOT set the trait bslma::UsesBslmaAllocator, but the trait will automatically be true if ALLOC is convertible from bslma::Allocator * (e.g., if it is an instantiation of bsl::allocator). Furthermore, this class is not bitwise-moveable, and will assert on destruction if it has been moved. This class is primarily provided to facilitate testing of templates by defining a simple type representative of user-defined types using a standard allocator.

Attributes

Name Type Default
------------------ ----------- -------
data int 0

Usage

This section illustrates intended use of this component.

Example 1: Printing the Supported Traits of This Type

Suppose we wanted to print the supported traits of this test type.

First, we create a function template printTypeTraits with a parameterized TYPE:

/// Prints the traits of the parameterized `TYPE` to the console.
template <class TYPE>
void printTypeTraits()
{
printf("Type defines bslma::UsesBslmaAllocator.\n");
}
else {
printf("Type does not define bslma::UsesBslmaAllocator.\n");
}
printf("Type defines bslmf::IsBitwiseMoveable.\n");
}
else {
printf("Type does not define bslmf::IsBitwiseMoveable.\n");
}
}
Definition bslma_usesbslmaallocator.h:343
Definition bslmf_isbitwisemoveable.h:718

Next, we create an STL-style allocator:

/// An STL-compliant allocator type.
template <class TYPE>
struct StlAllocator {
typedef TYPE value_type;
TYPE *allocate(std::size_t);
void deallocate(TYPE *, std::size_t);
};

Now, we invoke the printTypeTraits function template using AllocTestType as the parameterized TYPE, using both bsl::allocator and StlAllocator as the ALLOC parameter:

printTypeTraits<StdAllocTestType<bsl::allocator<int> > >();
printTypeTraits<StdAllocTestType<StlAllocator<int> > >();

Finally, we observe the console output:

Type does not define bslmf::IsBitwiseMoveable.
Type does not define bslmf::IsBitwiseMoveable.