如果标准容器元素类型和std :: allocator类型不同,那么它是错误的吗? [英] Is it wrong if the standard container element type and std::allocator type are different?

查看:175
本文介绍了如果标准容器元素类型和std :: allocator类型不同,那么它是错误的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里(很旧):


同样重要的是,用于分配器模板
参数的类型和用作标准
容器中的元素类型的类型是一致的。例如:

It's also important that the type used for the allocator template parameter and the type used as the element type in a standard container agree. For instance:

std::list<int, std::allocator<long> >                    // Wrong!

无效。

问题

上述语句是否正确(或是否正确)?我做的任何测试似乎工作正常,无论我在 T std :: allocator 中。例如, std :: vector< int,std :: allocator< std :: string>> 编译和工作精细推回和擦除元素等
(从我理解 std :: allocator< std :: string> :: rebind< int> :: other 是使这个工作的魔法)。

Is the above statement correct (or was it ever correct)? Any tests I have done seem to work fine no matter what I put for T in std::allocator. For example, std::vector<int, std::allocator<std::string>> compiled and worked fine pushing back and erasing elements, etc. (From what I understand std::allocator<std::string>::rebind<int>::other is the magic that makes this work).

推荐答案

编辑:在[containers.requirements.general]中,Allocator感知容器需求指示 allocator_type: :value_type Container :: value_type 相同。

In [containers.requirements.general], the Allocator-aware container requirements indicate that the allocator_type::value_type is the same as Container::value_type.

传递具有不同 value_type 的分配器类型,尽管至少一个实现仅使用 allocator_traits< ...> :: rebind< value_type> 以获取正确的分配器。

So it's ill formed to pass in an allocator type with a different value_type, although at least one implementation simply uses allocator_traits<...>::rebind<value_type> to get the correct allocator.

这篇关于如果标准容器元素类型和std :: allocator类型不同,那么它是错误的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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