gpt4 book ai didi

c++ - 为什么容器分配器需要指定它们分配的类型?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:10:17 26 4
gpt4 key购买 nike

如果我为容器使用自定义分配器,我不喜欢必须重复包含的类型名:

template<typename T, size_t MyAllocatorArgument>
struct MyAllocator : public std::allocator<T>
{
// ... Usual allocator implementation
};

typedef std::vector<int, MyAllocator<int, 42>> int_container;
typedef std::vector<int, MyAllocator<long, 12>> int_container_wrong_allocator;

根据标准,第二行是未定义的行为,尽管大多数实现会将分配器重新绑定(bind)到正确的类型。

我的问题是,既然要求容器和分配器属于同一类型,为什么没有适当的标准机制来强制执行(或完全避免)并消除用户错误的可能性?

例如,标准可以强制使用rebind(以有效地使分配器模板参数冗余),或者可以使用如下所示的模式,以便用户仅提及包含的类型名一次:

template<size_t MyAllocatorArgument>
struct MyAllocator
{
// This would be something every allocator is required to expose.
template<typename T>
struct TypedAllocator : public std::allocator<T>
{
// This is where the normal implementation of the allocator would go.
// allocate, deallocate etc.
};
};

template<typename T, typename UntypedAllocator>
struct Container
{
// All containers would do this to get the actual allocator type they would use.
typedef typename UntypedAllocator::template TypedAllocator<T> TypedAllocator;

Container() : m_allocator(TypedAllocator()) {}

void useAllocator()
{
m_allocator.allocate();
// ... or whatever else containers need to do with allocators.
}

TypedAllocator m_allocator;
};

void allocator_test()
{
// Allocated type name isn't mentioned at point of use of container;
// only once for the container. The container does all the work.
Container<int, MyAllocator<42>> c1;
}

最佳答案

这是一个很好的问题,您的建议是标准方案的一种可能替代方案。另一种方法是使用模板模板参数:

template<typename T>
class AnAllocator
{ ... };

template<typename T, template <typename> class Alloc = std::allocator>
class Vector
{
typedef Alloc<T> allocator_type;
...
};

Vector<int, AnAllocator> v;

分配器接口(interface)是在模板模板参数成为语言的一部分之前设计的,因此这不是一个选项。

如果今天设计分配器 API,很多事情会以不同的方式完成,不幸的是,我们坚持使用现有的(以及由于试图以半兼容的方式扩展它而导致的持续复杂性) .

关于c++ - 为什么容器分配器需要指定它们分配的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22908681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com