std::uses_allocator

From cppreference.com
< cpp‎ | memory
Memory management library
(exposition only*)
Uninitialized memory algorithms
(C++17)
(C++17)
(C++17)
(C++20)
Constrained uninitialized
memory algorithms
(C++20)
C Library

Allocators
(C++11)
uses_allocator
(C++11)
Memory resources
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
Uninitialized storage
(until C++20*)
(until C++20*)
(until C++20*)
Explicit lifetime management
Defined in header <memory>
template < class T, class Alloc >
struct uses_allocator;
(since C++11)

If T has a nested type allocator_type which is convertible from Alloc, the member constant value is true. Otherwise value is false

Helper variable template

template < class T, class Alloc >
constexpr bool uses_allocator_v = uses_allocator<T, Alloc> :: value ;
(since C++17)

Inherited from std::integral_constant

Member constants

value
[static]
true if T uses allocator Alloc, false otherwise
(public static member constant)

Member functions

operator bool
converts the object to bool, returns value
(public member function)
operator()
(C++14)
returns value
(public member function)

Member types

Type Definition
value_type bool
type std::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
  • Otherwise, std::uses_allocator_v<T, Alloc> is true
  • if T uses the leading-allocator convention (is invocable as T(std::allocator_arg, alloc, args...)
  • if T uses the trailing-allocator convention (is invocable as T(args..., alloc)
  • Otherwise, the program is ill-formed (this means std::uses_allocator_v<T, Alloc> is true

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

(since C++20)

Specializations

Given a program-defined type T that does not have a nested allocator_type, a program can specialize std::uses_allocator to derive from std::true_type for T

  • T has a constructor which takes std::allocator_arg_t as the first argument, and Alloc
  • 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:

specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
(C++11) (until C++17)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)
specializes the std::uses_allocator type trait
(class template specialization)

Notes

This type trait is used by std::tuple, std::scoped_allocator_adaptor, and std::pmr::polymorphic_allocator

See also

a tag used to select allocator-aware constructors
(tag)
prepares the argument list matching the flavor of uses-allocator construction required by the given type
(function template)
creates an object of the given type by means of uses-allocator construction
(function template)
creates an object of the given type at specified memory location by means of uses-allocator construction
(function template)
implements multi-level allocator for multi-level containers
(class template)