May std :: vector使用小缓冲区优化? [英] May std::vector make use of small buffer optimization?

查看:180
本文介绍了May std :: vector使用小缓冲区优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的同事今天是否std :: vector可以实现使用小缓冲区优化。通过查看C ++ 11草稿,我在23.3.1p8阅读

I was wondering with my colleague today whether std::vector can be implemented to make use of small buffer optimization. By looking into the C++11 draft, I read at 23.3.1p8


表达式a.swap(b)和b不是数组的标准容器类型,应交换a和b的值,而不调用对各个容器元素的任何移动,复制或交换操作。

The expression a.swap(b), for containers a and b of a standard container type other than array, shall exchange the values of a and b without invoking any move, copy, or swap operations on the individual container elements.

这首先似乎违反了小缓冲区优化,但是在as-if规则下,我们仍然可以做小缓冲区非类类型的优化(因为我们不能观察到拷贝)。下一个文本似乎更难以傻瓜

That at first seems to outlaw small buffer optimization, but under the as-if rule, we would be allowed to still do small buffer optimization for non-class types (since we cannot observe the copy being done). The next text appears to be harder to "fool"


每个在交换之前引用一个容器中的元素的迭代器都是指同一个元素在交换后的另一个容器中。

Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap.

这是否足以阻止对std :: vector实现小缓冲区优化?是否还有其他路障或者最终可能有一个带有SBO的std :: vector?

Is this sufficient to prevent implementing the small buffer optimization for std::vector? Are there any other road-blocks or is it eventually possible to have a std::vector with SBO?

推荐答案

/ p10 / b6:

23.2.1 / p10 / b6:


除非另有规定...

Unless otherwise specified ...


  • no swap()函数使任何引用,指针或迭代器引用要交换的容器的元素无效。
    ...

c> vector 。所以这违反了SBO 向量

Nowhere does it "specify otherwise" for vector. So this outlaws the SBO for vector.

string 不受此规则约束,因为它在21.4.1 / p6中有指定:

string is not bound by this rule because it does "specify otherwise" in 21.4.1/p6:


引用,指针和迭代器
basic_string序列的元素可能由于以下对
basic_string对象的使用而失效:

References, pointers, and iterators referring to the elements of a basic_string sequence may be invalidated by the following uses of that basic_string object:


  • ^ 234

234)例如,作为一个参数,引用非const basic_string作为参数。非成员函数的参数swap()
(21.4.8.8),运算符>>()(21.4.8.9)和getline()(21.4.8.9),或者作为
参数basic_string :: swap()

234) For example, as an argument to non-member functions swap() (21.4.8.8), operator>>() (21.4.8.9), and getline() (21.4.8.9), or as an argument to basic_string::swap()

这篇关于May std :: vector使用小缓冲区优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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