跳到主要内容

std::uses_allocator

定义于内存
自 C++11 起
template< class T, class Alloc > struct uses_allocator;

如果 T 有一个成员 typedef allocator_type,并且它可以从 Alloc 转换
***或者它是 std::experimental::erased_type 的别名***(库基础 TS),
则成员常量 value 为 true。否则 value 为 false

辅助变量模板

自 C++17 起
template< class T, class Alloc >
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
继承自 std::integral_constant

成员常量

公有value静态如果 T 使用分配器 Alloc,则为 true,否则为 false

成员函数

公有operator bool将对象转换为 bool,返回 value
公有operator()(C++14)返回 value

成员类型

公有类型定义
公有value_typebool
公有typestd::integral_constant<bool, value>

使用分配器构造

将分配器 alloc 传递给某个类型 T 的构造函数有三种约定

特化

对于没有成员 typedef allocator_type 但满足以下两个要求之一的类型,允许自定义特化类型特征 std::uses_allocator

  1. T 有一个构造函数,它将 std::allocator_arg_t 作为第一个参数,将 Alloc 作为第二个参数。
  2. T 有一个构造函数,它将 Alloc 作为最后一个参数。

在上面,Alloc 是一种满足 Allocator 的类型
或者是一种可转换为 std::experimental::pmr::memory_resource* 的指针类型(库基础 TS)。

标准库已提供以下特化

公有std::uses_allocator<std::tuple> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::queue> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::priority_queue> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::stack> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_map> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_set> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_multimap> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_multiset> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::function> (C++11) (直至 C++17)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::promise> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::packaged_task> (C++11) (直至 C++17)特化 std::uses_allocator 类型特征

备注

此类型特征由 std::tuplestd::scoped_allocator_adaptorstd::pmr::polymorphic_allocator 使用。它也可由自定义分配器或包装器类型使用,以确定正在构造的对象或成员本身是否能够使用分配器(例如,是否为容器),在这种情况下,应将分配器传递给其构造函数。

std::uses_allocator

定义于内存
自 C++11 起
template< class T, class Alloc > struct uses_allocator;

如果 T 有一个成员 typedef allocator_type,并且它可以从 Alloc 转换
***或者它是 std::experimental::erased_type 的别名***(库基础 TS),
则成员常量 value 为 true。否则 value 为 false

辅助变量模板

自 C++17 起
template< class T, class Alloc >
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
继承自 std::integral_constant

成员常量

公有value静态如果 T 使用分配器 Alloc,则为 true,否则为 false

成员函数

公有operator bool将对象转换为 bool,返回 value
公有operator()(C++14)返回 value

成员类型

公有类型定义
公有value_typebool
公有typestd::integral_constant<bool, value>

使用分配器构造

将分配器 alloc 传递给某个类型 T 的构造函数有三种约定

特化

对于没有成员 typedef allocator_type 但满足以下两个要求之一的类型,允许自定义特化类型特征 std::uses_allocator

  1. T 有一个构造函数,它将 std::allocator_arg_t 作为第一个参数,将 Alloc 作为第二个参数。
  2. T 有一个构造函数,它将 Alloc 作为最后一个参数。

在上面,Alloc 是一种满足 Allocator 的类型
或者是一种可转换为 std::experimental::pmr::memory_resource* 的指针类型(库基础 TS)。

标准库已提供以下特化

公有std::uses_allocator<std::tuple> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::queue> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::priority_queue> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::stack> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_map> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_set> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_multimap> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::flat_multiset> (C++23)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::function> (C++11) (直至 C++17)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::promise> (C++11)特化 std::uses_allocator 类型特征
公有std::uses_allocator<std::packaged_task> (C++11) (直至 C++17)特化 std::uses_allocator 类型特征

备注

此类型特征由 std::tuplestd::scoped_allocator_adaptorstd::pmr::polymorphic_allocator 使用。它也可由自定义分配器或包装器类型使用,以确定正在构造的对象或成员本身是否能够使用分配器(例如,是否为容器),在这种情况下,应将分配器传递给其构造函数。