gpt4 book ai didi

c++ - STL 容器 : Constructor's Allocator parameter and scoped allocators

转载 作者:IT老高 更新时间:2023-10-28 22:37:52 43 4
gpt4 key购买 nike

STL 容器有一个模板参数可以选择自定义分配器。花了一段时间,但我想我明白它是如何工作的。不知何故,它并不是很好,因为给定的分配器类型没有直接使用,而是反弹到另一种类型的分配器。我终于可以使用它了。

看完API我认识到也有可能将分配器作为构造函数参数。但是,如果容器在内部从模板参数重新绑定(bind)给定的分配器,我怎么知道容器使用哪种分配器?

此外,我读到 C++11 现在使用范围分配器,它允许将容器的分配器重用于其包含的容器。启用了作用域分配器的容器的实现与不知道作用域容器的容器的实现有何不同?

很遗憾,我找不到任何可以解释这一点的东西。感谢您的回答!

最佳答案

But how do I know which kind of allocator the container uses, if it internally rebinds the given allocator from the template parameter?

始终提供 Allocator<T>到构造函数(其中 T 是容器的 value_type)。容器会将其转换为 Allocator<U>U 的情况下是必需的是容器的一些内部数据结构。 Allocator需要提供这样的转换构造函数,例如:

template <class T> class allocator {
...
template <class U> allocator(const allocator<U>&);

Additionally I read that C++11 now uses scoped allocators which allow to reuse the allocator of a container for its containing containers.

嗯,更准确地说,C++11 有一个名为 scoped_allocator_adaptor分配器适配器 :

template <class OuterAlloc, class... InnerAllocs>
class scoped_allocator_adaptor : public OuterAlloc
{
...
};

来自 C++11:

The class template scoped_allocator_adaptor is an allocator template that specifies the memory resource (the outer allocator) to be used by a container (as any other allocator does) and also specifies an inner allocator resource to be passed to the constructor of every element within the container. This adaptor is instantiated with one outer and zero or more inner allocator types. If instantiated with only one alloca- tor type, the inner allocator becomes the scoped_allocator_adaptor itself, thus using the same allocator resource for the container and every element within the container and, if the elements themselves are con- tainers, each of their elements recursively. If instantiated with more than one allocator, the first allocator is the outer allocator for use by the container, the second allocator is passed to the constructors of the container’s elements, and, if the elements themselves are containers, the third allocator is passed to the elements’ elements, and so on. If containers are nested to a depth greater than the number of allocators, the last allocator is used repeatedly, as in the single-allocator case, for any remaining recursions. [Note: The scoped_allocator_adaptor is derived from the outer allocator type so it can be substituted for the outer allocator type in most expressions. — end note ]

因此,如果您指定 scoped_allocator_adaptor,您只会获得作用域分配器行为。作为容器的分配器。

How does the implementation of a scoped allocator enabled container roughly differs from one that is not aware of scoped containers?

关键是容器现在通过一个名为 allocator_traits 的新类来处理它的分配器。而不是直接处理分配器。并且容器必须使用allocator_traits对于某些操作,例如构造和破坏value_type s 在容器中。容器不得直接与分配器对话。

例如,分配器可以提供一个名为 construct 的成员这将使用给定的参数在某个地址构造一个类型:

template <class T> class Allocator {
...
template<class U, class... Args>
void construct(U* p, Args&&... args);
};

如果分配器不提供此成员,allocator_traits将提供默认实现。无论如何,容器必须构造所有value_type s 使用这个 construct函数,但通过 allocator_traits 使用它,而不是使用 allocator直接:

allocator_traits<allocator_type>::construct(the_allocator, *ugly details*);

scoped_allocator_adaptor提供定制construct allocator_traits 的函数将转发到哪个利用uses_allocator特征并将正确的分配器传递给 value_type构造函数。容器仍然对这些细节一无所知。容器只需要知道它必须构造value_type使用 allocator_traits construct功能。

容器必须处理更多细节才能正确处理有状态分配器。虽然这些细节也是通过让容器不做任何假设而是通过allocator_traits获取所有属性和行为来处理的。 .容器甚至不能假设 pointerT* .而是通过询问 allocator_traits 找到这种类型。它是什么。

简而言之,要构建一个 C++11 容器,请学习 allocator_traits .然后,当您的客户使用 scoped_allocator_adaptor 时,您将免费获得作用域分配器行为。 .

关于c++ - STL 容器 : Constructor's Allocator parameter and scoped allocators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12556638/

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