为什么标准容器要求allocator_type :: value_type为元素类型? [英] Why do standard containers require allocator_type::value_type to be the element type?

查看:418
本文介绍了为什么标准容器要求allocator_type :: value_type为元素类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:弃用std :: allocator< void> .

对于 std::vector std::list (重点是我):

The following description about template parameter Allocator is found for both std::vector and std::list (emphasis mine):

分配器,用于获取/释放内存并用于 构造/销毁该内存中的元素.类型必须符合 分配器的要求. 如果未定义行为,则为 Allocator::value_typeT 不同.

An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T.

最后一句话对我来说没有意义.如果需要特定的value_type,难道不是只是重新分配分配器?

The last sentence does not make sense to me. If a specific value_type is required, couldn't it just do an allocator rebind?

推荐答案

原因主要是历史性的-在C ++ 11添加allocator_traits之前,重新绑定更为复杂. 网络TS 定义了"proto-allocator"的概念([async.reqmts.proto.allocator]),其中绑定总是在使用之前进行,因此似乎有一天该要求会放宽.

The reason is mostly historical—rebinding was more complicated before C++11 added allocator_traits. The Networking TS defines a "proto-allocator" concept ([async.reqmts.proto.allocator]) where rebinding is always applied before any use, so it seems likely that the requirement will be relaxed someday.

这篇关于为什么标准容器要求allocator_type :: value_type为元素类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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