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

问题描述

从(很旧):

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类型不同,那么它是错误的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 12:29