STD ::列表对其分配器参数有什么作用
What does the std::list do with its allocator argument?
std::list
是通过分配类型进行参数化的,它大概可以重新启动以分配列表节点而不是 T
s。那么,它与传递给构造函数的分配器类型的对象有什么关系?
template <class T, class Alloc = std::allocator<T>>
class list
{
class node { ... T element; ... };
using real_alloc =
typename std::allocator_traits<Alloc>::template rebind_alloc<node>;
real_alloc m_allocator;
public:
list(const Alloc&);
};
在上述代码中,我们需要将m_allocator
初始化为节点分配器,但是将构造函数赋予T
分配器。我们只是将T
分配器放在地板上,还是以某种方式使用?
,因为C++11
分配器可以说明。您需要使用用户提供的内部分配器来构建内部分配器。明确需要从typename std::allocator_traits<Alloc>::template rebind_alloc<U>
构造分配器。
列表构造函数的实现应该看起来像:
template<class T, class Alloc>
class list
{
struct node_type {/*etc*/}; //internal
using allocator_type_internal = typename std::allocator_traits<Alloc>::template rebind_alloc<node_type>;
// etc.
allocator_type_internal m_allocator; //Note we use allocator_type, not Alloc.
};
list::list(const Alloc& alloc ) :
m_allocator(alloc)
{ /* initialize sentinel node, etc. */ }
虽然一个良好的实现将对分配器使用空基本优化。
T
分配器仍用于construct()
和destroy()
内部节点的T
部分。在构建过程中添加自定义参数可能需要使用这些操作,例如,可选地将合适的分配器参数转发到构造的对象。
相关文章:
- archive_read_support_filter_all() 有什么作用?
- 当我们为(;;) 写作时,它做了什么?for 循环中的双分号有什么作用?
- 这个递归函数有什么作用?运行时的复杂性是多少?
- DrawShadow 中的 Elevation 究竟有什么作用?
- 测试"stream >> char"有什么作用?
- "uFlags &= ~CHN_PANNING"命令有什么作用?
- GCC 的 -Wpsabi 选项究竟有什么作用?压制它意味着什么?
- int** a = new int*[n]();这个函数有什么作用?
- C++ 此函数调用之前的(void)有什么作用?
- i+=(i&-i) 有什么作用?它是便携式的吗?
- 这个size_t在 lambda 中有什么作用?C++代码
- 数组下标运算符 ([ ]) 对数组有什么作用?
- 结构之外的节点 * head 有什么作用?
- 重载括号运算符有什么作用?
- clang的"-Ofast"选项在实践中有什么作用,特别是对于与gcc的任何差异?
- std::make_unique_for_overwrite() 对 std::make_unique() 有什么作用?
- 运算符 delete[] 有什么作用?
- 下面的代码有什么作用?c++
- 这个陈述'p = I.ptr<uchar>(i);有什么作用?
- 这个嵌套的名称空间定义有什么作用