C ++ 11和C ++ 03不同在支持std :: string的小字符串优化? [英] C++11 and C++03 differs in support for small string optimization for std::string?

查看:161
本文介绍了C ++ 11和C ++ 03不同在支持std :: string的小字符串优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11标准的兼容性附录中,从C ++ 03到C ++ 11的更改描述如下:

In the compatibility appendix of the C++11 standard, one of the change from C++03 to C++11 is described as below:


C.2.11第21条:字符串库

21.4.1

更改:松开basic_string无效规则

原因:允许小字符串优化。

对原始特性的影响:有效C ++ 2003代码可以在本标准中以不同的方式执行
。一些const成员
函数,如数据和c_str,不再使迭代器无效。

C.2.11 Clause 21: strings library
21.4.1
Change: Loosen basic_string invalidation rules
Rationale: Allow small-string optimization.
Effect on original feature: Valid C++ 2003 code may execute differently in this International Standard. Some const member functions, such as data and c_str, no longer invalidate iterators.

迭代器无效规则< c $ c> std :: string 已经从C ++ 03改为C ++ 11,因为 data() c_str()不允许无效迭代器,但我不知道如何导致允许小字符串优化的原理?在C ++ 11之前不允许使用SSO?

The iterator invalidation rule of std::string does have changed from C++03 to C++11 in that data() and c_str() is not allowed to invalidate iterators anymore, but I don't know how that leads to the rationale of "allow small-string optimization"? Wasn't SSO already allowed prior to C++11?

在我发现之前我知道的C ++ 11和C ++ 03之间的std :: string 是兼容性附录中的SSO注释:

Two of the differences of std::string between C++11 and C++03 that I knew of before I came across that SSO notes in the compatibility appendix are:


  • 元素保证在C ++ 11中连续存储

  • COW实现不是C ++ 11中的一个选项(某些操作, code> operator [] 不允许无效迭代器)

  • Elements are guaranteed to be stored contiguously in C++11
  • COW implementation is not an option in C++11 (certain operations such as operator[] is not allowed to invalidate iterators)

11作为对连续存储的改变的直接结果的 c_str() data()的非无效保证。现在看来有一些与SSO实现的连接,我想知道后台面逻辑的细节。感谢。

I took C++11's non-invalidating guarantee of c_str() and data() as the direct result of the change towards contiguous storage. Now it seems there is some connection with SSO implementation, and I would like to know the detail of the behind-scene logic. Thanks.

推荐答案

我相信你误解了改变:这不是指 data c_str ,而是 swap

I believe you are misinterpreting the "Change": this is not referring to the invalidation rules of data and c_str, but rather those of swap.

这个问题给了我这个想法

由于 basic_string ',我认为我是正确的。 s交换可以使迭代器无效,可以实现SSO。在C ++ 03中, basic_string 是一个可逆容器,这意味着它必须符合 swap container:

Since basic_string's swap can invalidate iterators, one can implement a SSO. In C++03, basic_string was a reversible container, which means it had to comply with this requirement for swapping containers:


表达式a.swap(b)对于除array之外的标准容器类型的容器a和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.

我可能是非常错误,IANALL(我不是语言律师)。

I may be immensely wrong though, IANALL (I am not a language lawyer).

这篇关于C ++ 11和C ++ 03不同在支持std :: string的小字符串优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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