问题描述
新的 C++11 标准需要 STL 实现来支持容器中的有状态分配器.现在主要的 STL 实现(Visual Studio 2008、2010、libstdc++)是否符合这个要求?我在 MSDN 或 libstdc++ 文档中一无所获.
The new C++11 standard requires STL implementations to support stateful allocators in containers. Do main STL implementations (Visual Studio 2008, 2010, libstdc++) comply to this requirement now? I found nothing about this in MSDN or in libstdc++ documentation.
推荐答案
看起来 STL 容器中的状态分配器功能已经得到广泛支持.在大多数情况下,分配器的状态性不会造成麻烦.尚未得到广泛支持的是新标准处理问题情况的方式(交换容器(是否也交换分配器)、列表拼接).
Looks like the feature of stateful allocators in STL containers is widely supported already. In most cases statefullness of the allocator does not cause trouble. What is not widely supported yet is the new standard's way of handling the problematic situations (swap of a container(whether to swap the allocator too), splice of lists).
本帖 说:
在大多数当前标准库(包括 MS 使用的 Dinkumware's)的代码中,支持有状态分配器
in code for most current standard libraries (including Dinkumware's as used by MS), stateful allocators are supported
这一步(libstdc++,2004)说(如果我理解正确):
This tread (libstdc++, 2004) says (if i understood correctly):
我们已经支持 l1.get_allocator() != l2.get_allocator()
的分配器.我们不做任何特殊规定来检测 splice()
和 swap()
中的分配器.
We already support allocators where
l1.get_allocator() != l2.get_allocator()
. What we don't do is make any special provisions to detect those allocators insplice()
andswap()
.
这篇博文(libstdc++,2009)说:
This blog entry (libstdc++, 2009) says:
C++0x 模式下的现有容器现在与有状态分配器一起使用效率更高(即,在元素构造时不会动态创建分配器).
Existing containers in C++0x mode are now more efficient together with stateful allocators (i.e., no allocators are created on the fly at element construction time).
本文 谈到新的 libc++ 库:
所有容器都满足所有最新的分配器要求完全支持有状态分配器.– 优化了无状态分配器的空间.
All containers meet all of the latest allocator requirements which fully support stateful allocators. – Space for stateless allocators optimized away.
EASTL 支持全状态分配器.
这个线程 包含关于此功能的可移植性的有趣争议.
This thread contains an interesting dispute about how portable this feature is.
因此,大多数 STL 实现都支持有状态分配器,这意味着它们不会在后台创建分配器类型的其他实例,而是存储客户端提供的分配器实例,并且所有分配/解除分配都是通过它完成的.然而,他们处理 swap
ping 和 list::splice
的方式是无证的、不可移植的.
So most STL implementations support statefull allocators, which means that they do not create additional instances of the allocator type under the hood, but store the client-supplied allocator instance and all allocations/deallocations are done via that. However the way that they handle swap
ping and list::splice
is undocumented, non-portable.
更新:VS2008 的 STL 要求分配器具有模板化的复制构造函数,而 IMO 使自定义分配器的最重要用途变得不可能:简单的隔离存储.
UPDATE: VS2008's STL requires the allocators to have the templated copy constructor, which IMO makes the most important use of custom allocators impossible: simple segregated storage.
如果对 STL 中状态分配器的当前状态不满意,我建议考虑 Boost.Intrusive
和 Boost.Container
.
For whoever is not satisfied with the current state of stateful allocators in STL, I recommend to consider Boost.Intrusive
and Boost.Container
.
这篇关于编译器对 STL 容器中的状态分配器的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!