Skip to main content

std::uses_allocator

Defined inmemory
Since C++11
template< class T, class Alloc > struct uses_allocator;

If T has a member typedef allocator_type which is convertible from Alloc
***or is an alias of std::experimental::erased_type ***(library fundamentals TS),
the member constant value is true. Otherwise value is false.

Helper variable template

Since C++17
template< class T, class Alloc >
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
Inherited from std::integral_constant

Member constants

pubvaluestatictrue if T uses allocator Alloc, false otherwise

Member functions

puboperator boolconverts the object to bool, returns value
puboperator()(C++14)returns value

Member types

pubTypeDefinition
pubvalue_typebool
pubtypestd::integral_constant<bool, value>

Uses-allocator construction

There are three conventions of passing an allocator alloc to a constructor of some type T:

  • if T does not use a compatible allocator (std::uses_allocator_v<T, Alloc> is false), then alloc is ignored.
  • otherwise, std::uses_allocator_v<T, Alloc> is true, and
    • if T uses the leading-allocator convention (is invocable as T(std::allocator_arg, alloc, args...)), then uses-allocator construction uses this form
    • if T uses the trailing-allocator convention (is invocable as T(args..., alloc)), then uses-allocator construction uses this form
    • otherwise, the program is ill-formed (this means std::uses_allocator_v<T, Alloc> is true, but the type does not follow either of the two allowed conventions)
  • As a special case, std::pair is treated as a uses-allocator type even though std::uses_allocator is false for pairs (unlike e.g. std::tuple): see pair-specific overloads of std::pmr::polymorphic_allocator::construct and std::scoped_allocator_adaptor::construct  (until C++20) std::uses_allocator_construction_args  (since C++20)

    The utility functions std::make_obj_using_allocator, and std::uninitialized_construct_using_allocator may be used to explicitly create an object following the above protocol, and std::uses_allocator_construction_args can be used to prepare the argument list that matches the flavor of uses-allocator construction expected by the type.  (since C++20)

Specializations

Custom specializations of the type trait std::uses_allocator are allowed for types that do not have the member typedef allocator_type but satisfy one of the following two requirements:

  1. T has a constructor which takes std::allocator_arg_t as the first argument, and Alloc as the second argument.
  2. T has a constructor which takes Alloc as the last argument.

In the above, Alloc is a type that satisfies Allocator
or is a pointer type convertible to std::experimental::pmr::memory_resource* (library fundamentals TS).

The following specializations are already provided by the standard library:

pubstd::uses_allocator<std::tuple> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::queue> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::priority_queue> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::stack> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_map> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_set> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_multimap> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_multiset> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::function> (C++11) (until C++17)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::promise> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::packaged_task> (C++11) (until C++17)specializes the std::uses_allocator type trait

Notes

This type trait is used by std::tuple, std::scoped_allocator_adaptor, and std::pmr::polymorphic_allocator. It may also be used by custom allocators or wrapper types to determine whether the object or member being constructed is itself capable of using an allocator (e.g. is a container), in which case an allocator should be passed to its constructor.

std::uses_allocator

Defined inmemory
Since C++11
template< class T, class Alloc > struct uses_allocator;

If T has a member typedef allocator_type which is convertible from Alloc
***or is an alias of std::experimental::erased_type ***(library fundamentals TS),
the member constant value is true. Otherwise value is false.

Helper variable template

Since C++17
template< class T, class Alloc >
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
Inherited from std::integral_constant

Member constants

pubvaluestatictrue if T uses allocator Alloc, false otherwise

Member functions

puboperator boolconverts the object to bool, returns value
puboperator()(C++14)returns value

Member types

pubTypeDefinition
pubvalue_typebool
pubtypestd::integral_constant<bool, value>

Uses-allocator construction

There are three conventions of passing an allocator alloc to a constructor of some type T:

  • if T does not use a compatible allocator (std::uses_allocator_v<T, Alloc> is false), then alloc is ignored.
  • otherwise, std::uses_allocator_v<T, Alloc> is true, and
    • if T uses the leading-allocator convention (is invocable as T(std::allocator_arg, alloc, args...)), then uses-allocator construction uses this form
    • if T uses the trailing-allocator convention (is invocable as T(args..., alloc)), then uses-allocator construction uses this form
    • otherwise, the program is ill-formed (this means std::uses_allocator_v<T, Alloc> is true, but the type does not follow either of the two allowed conventions)
  • As a special case, std::pair is treated as a uses-allocator type even though std::uses_allocator is false for pairs (unlike e.g. std::tuple): see pair-specific overloads of std::pmr::polymorphic_allocator::construct and std::scoped_allocator_adaptor::construct  (until C++20) std::uses_allocator_construction_args  (since C++20)

    The utility functions std::make_obj_using_allocator, and std::uninitialized_construct_using_allocator may be used to explicitly create an object following the above protocol, and std::uses_allocator_construction_args can be used to prepare the argument list that matches the flavor of uses-allocator construction expected by the type.  (since C++20)

Specializations

Custom specializations of the type trait std::uses_allocator are allowed for types that do not have the member typedef allocator_type but satisfy one of the following two requirements:

  1. T has a constructor which takes std::allocator_arg_t as the first argument, and Alloc as the second argument.
  2. T has a constructor which takes Alloc as the last argument.

In the above, Alloc is a type that satisfies Allocator
or is a pointer type convertible to std::experimental::pmr::memory_resource* (library fundamentals TS).

The following specializations are already provided by the standard library:

pubstd::uses_allocator<std::tuple> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::queue> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::priority_queue> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::stack> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_map> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_set> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_multimap> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::flat_multiset> (C++23)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::function> (C++11) (until C++17)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::promise> (C++11)specializes the std::uses_allocator type trait
pubstd::uses_allocator<std::packaged_task> (C++11) (until C++17)specializes the std::uses_allocator type trait

Notes

This type trait is used by std::tuple, std::scoped_allocator_adaptor, and std::pmr::polymorphic_allocator. It may also be used by custom allocators or wrapper types to determine whether the object or member being constructed is itself capable of using an allocator (e.g. is a container), in which case an allocator should be passed to its constructor.