std::uses_allocator
定义于 | 内存 |
template< class T, class Alloc > struct uses_allocator;
如果 T 有一个成员 typedef allocator_type,并且它可以从 Alloc 转换
***或者它是 std::experimental::erased_type 的别名***(库基础 TS),
则成员常量 value 为 true
。否则 value 为 false
。
辅助变量模板
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_type | bool |
公有 | type | std::integral_constant<bool, value> |
使用分配器构造
将分配器 alloc 传递给某个类型 T 的构造函数有三种约定
- 如果 T 不使用兼容的分配器(
std::uses_allocator_v<T, Alloc>
为 false),则 alloc 被忽略。 - 否则,
std::uses_allocator_v<T, Alloc>
为 true,并且- 如果 T 使用前置分配器约定(可调用为
T(std::allocator_arg, alloc, args...)
),则使用分配器构造将使用此形式 - 如果 T 使用后置分配器约定(可调用为
T(args..., alloc)
),则使用分配器构造将使用此形式 - 否则,程序格式错误(这意味着
std::uses_allocator_v<T, Alloc>
为 true,但该类型不遵循两种允许约定中的任何一种)
- 如果 T 使用前置分配器约定(可调用为
- 作为特例,std::pair 被视为使用分配器类型,即使 std::uses_allocator 对 pair 为 false(与 std::tuple 等不同):请参阅 std::pmr::polymorphic_allocator::construct 和 std::scoped_allocator_adaptor::construct 的 pair 特有重载 (直至 C++20) std::uses_allocator_construction_args (自 C++20 起)
实用函数 std::make_obj_using_allocator 和 std::uninitialized_construct_using_allocator 可用于显式创建遵循上述协议的对象,而 std::uses_allocator_construction_args 可用于准备与类型预期使用分配器构造方式匹配的参数列表。 (自 C++20 起)
特化
对于没有成员 typedef allocator_type 但满足以下两个要求之一的类型,允许自定义特化类型特征 std::uses_allocator
- T 有一个构造函数,它将 std::allocator_arg_t 作为第一个参数,将 Alloc 作为第二个参数。
- 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::tuple、std::scoped_allocator_adaptor 和 std::pmr::polymorphic_allocator 使用。它也可由自定义分配器或包装器类型使用,以确定正在构造的对象或成员本身是否能够使用分配器(例如,是否为容器),在这种情况下,应将分配器传递给其构造函数。