std::uses_allocator
在标头
<memory> 定义 |
||
template< class T, class Alloc >
struct uses_allocator; |
(C++11 起) | |
如果 T
拥有嵌套类型 allocator_type
,且该类型能从 Alloc
转换,那么成员常量 value
是 true。否则 value
是 false。
辅助变量模板
template< class T, class Alloc >
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value; |
(C++17 起) | |
继承自 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,但类型不遵守两种允许的约定之一)。
- 如果
- 作为特例,std::pair 被当做使用分配器类型,虽然
std::uses_allocator
对pair
为false
(与 std::tuple 等不同);见 std::polymoprhic_allocator::construct 与 std::scoped_allocator_adaptor::construct (C++20 前)std::uses_allocator_construction_args (C++20 起) 的pair
特定重载。
工具函数 std::make_obj_using_allocator 和 std::uninitialized_construct_using_allocator 可用于显式创建遵循上述协议的对象,而 std::uses_allocator_construction_args 能用于准备匹配类型所期待的使用分配器构造的形式的实参列表。 |
(C++20 起) |
特化
给定某个没有嵌套 allocator_type
的由程序定义的类型 T
,只要满足以下任意条件,程序就可以为 T
特化 std::uses_allocator
,使其从 std::true_type 派生:
T
拥有接收 std::allocator_arg_t 为首个实参,Alloc
为第二实参的构造函数。T
拥有接收Alloc
为最后实参的构造函数。
上述情况中,Alloc
是满足分配器 (Allocator) 要求的类型,或是可转换到 std::experimental::pmr::memory_resource* 的指针类型 (库基础 TS)。
标准库已提供下列特化:
(C++11)
|
特化 std::uses_allocator 类型特征 (类模板特化) |
(C++11)
|
特化 std::uses_allocator 类型特征 (类模板特化) |
特化 std::uses_allocator 类型特征 (类模板特化) |
|
(C++11)
|
特化 std::uses_allocator 类型特征 (类模板特化) |
特化 std::uses_allocator 类型特征 (类模板特化) |
|
特化 std::uses_allocator 类型特征 (类模板特化) |
|
特化 std::uses_allocator 类型特征 (类模板特化) |
|
特化 std::uses_allocator 类型特征 (类模板特化) |
|
(C++11) (C++17 前)
|
特化 std::uses_allocator 类型特征 (类模板特化) |
特化 std::uses_allocator 类型特征 (类模板特化) |
|
(C++11)(C++17 前)
|
特化 std::uses_allocator 类型特征 (类模板特化) |
注解
std::tuple、std::scoped_allocator_adaptor 和 std::pmr::polymorphic_allocator 使用这个类型特征。自定义分配器或封装类型也可以使用它,以确定要构造的对象或成员是否有能力使用分配器(例如是容器的情况),该情况下应传递分配器给它的构造函数。
参阅
(C++11)
|
用于选择具分配器的构造函数的标签 (标签) |
(C++20)
|
准备好与给定类型所要求的使用分配器构造的形式向匹配的实参列表 (函数模板) |
(C++20)
|
以使用分配器构造的手段创建给类型的对象 (函数模板) |
以使用分配器构造的手段在指定的内存位置创建给定类型的对象 (函数模板) |
|
(C++11)
|
为多级容器实现的多级分配器 (类模板) |