一些 STL 容器的 std::allocator 不匹配 [英] mismatched std::allocator for some of STL containers

查看:21
本文介绍了一些 STL 容器的 std::allocator 不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用不匹配的 std::allocator 特化(当然,除了 void 的特化)作为 STL 容器的模板参数(不是全部)在技术上是否有效,但在下面列举加上 unordered_(multi)map/set)?以下代码编译正常.

#include <list>#include <forward_list>#include <deque>#include <设置>#include <地图>主函数(){struct A { 布尔运算符 <(A) 常量 { 返回真;} };结构 B {};结构 C {};标准::列表

我相信这是因为分配器立即反弹到底层节点类型,与其源类型没有任何关系.

解决方案

我会说这是 UB(至少在 C++11 中),因为指定了一个具有不同 value_type 的 >value_type 违反了 allocator-aware 容器要求,这意味着这些实例不符合一般容器要求.此外,我在 C++11 标准中找不到任何内容表明分配器类型将从作为模板参数提供的类型中重新绑定.

<小时>

 1.[container.requirements.general] 节告诉我们:

<块引用>

13) 本条款和 (21.4) 中定义的所有容器(数组除外)都满足分配器感知容器的附加要求,如表 99 中所述.

 2.分配器感知容器要求说:

<块引用>

要求:allocator_type::value_typeX::value_type 相同.

  1. [default.allocator]部分指定

<块引用>

typedef T value_type;

作为命名空间 stdallocator 模板的成员.

 4.[multimap.overview] 部分包含:

模板,class Allocator = 分配器;>>类多图{[...]typedef 分配器 allocator_type;[...]};

(对于其他容器也有类似的发现.)

Is it technically valid to use mismatched std::allocator specialization (surely, except its specialization for void) as a template parameter for STL containers (not all of them, but enumerated below plus unordered_(multi)map/set)? Following code compiles fine.

#include <list>
#include <forward_list>
#include <deque>
#include <set>
#include <map>

int main()
{
    struct A { bool operator < (A) const { return true; } };
    struct B {};
    struct C {};
    std::list< A, std::allocator< C > > l;
    std::forward_list< A, std::allocator< C > > fl;
    std::deque< A, std::allocator< C > > d;
    std::set< A, std::less< A >, std::allocator< C > > s;
    std::multiset< A, std::less< A >, std::allocator< C > > ms;
    std::map< A, B, std::less< A >, std::allocator< C > > m;
    std::multimap< A, B, std::less< A >, std::allocator< C > > mm;
}

I believe this is due to allocator being immediately rebound to underlying node type without any relation to its source type.

解决方案

I'd say this is UB (at least in C++11) because specifying an allocator which has a different value_type from the value_type of the container violates the allocator-aware container requirements which means those instances do not conform to the general container requirements. Furthermore, I can't find anything in the C++11 standard that says that the allocator types are to be rebound from the type provided as template parameter.


 1. Section [container.requirements.general] tells us:

13) All of the containers defined in this Clause and in (21.4) except array meet the additional requirements of an allocator-aware container, as described in Table 99.

 2. The Allocator-aware container requirements says:

Requires: allocator_type::value_type is the same as X::value_type.

  1. Section [default.allocator] specifies

typedef T value_type;

as a member of the allocator template in namespace std.

 4. Section [multimap.overview] contains:

template <class Key, class T, class Compare = less<Key>,
    class Allocator = allocator<pair<const Key, T> > >
class multimap {
    [...]
    typedef Allocator allocator_type;
    [...]
 };

(With similar findings for the other containers.)

这篇关于一些 STL 容器的 std::allocator 不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆