本文介绍了为什么分配器是向量中的常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vector 在每种类型的构造函数中都有这个

vector has this in every type of constructor

const allocator_type& alloc = allocator_type()

为什么是常量?我看不出这有什么用.我可以看到传入一个分配器,因此多个向量可以共享同一个池,但与另一组向量分开.但是使用 const 是不是意味着他们只会复制实例数据?复制池或其他任何东西似乎都没有用.

Why is it const? I can't see how that'd be useful. I can see passing in an allocator so multiple vectors can share the same pool but be grouped away from another bunch of vectors. However with const wouldnt that mean they'd only copy the instance data? copying a pool or whatever it is doesn't seem to be useful.

为什么是常量?

推荐答案

分配器应该具有值语义,这意味着向量按值存储它(注意 get_allocator() 按值返回).所以构造函数可以很容易地通过 const 引用获取分配器并复制它.

Allocators are supposed to have value semantics, which means the vector stores it by value (notice that get_allocator() returns by value). So the constructor can easily take the allocator by const reference and just copy it.

这篇关于为什么分配器是向量中的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:43
查看更多