COW std :: string实现在C ++ 11中的合法性 [英] Legality of COW std::string implementation in C++11

查看:140
本文介绍了COW std :: string实现在C ++ 11中的合法性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我的理解,copy-on-write不是在C ++ 11中实现一个符合的 std :: string 的可行方法,

It had been my understanding that copy-on-write is not a viable way to implement a conforming std::string in C++11, but when it came up in discussion recently I found myself unable to directly support that statement.

我要纠正C ++ 11不允许基于COW的实现 std :: string

Am I correct that C++11 does not admit COW based implementations of std::string?

如果是这样,这个限制是在新标准中的哪里明确说明的?

If so, is this restriction explicitly stated somewhere in the new standard (where)?

或者这个限制意味着,它是对 std :: string 的新要求的组合作用,排除了COW基于 std :: string 的实现。在这种情况下,我会对'C ++ 11有效地禁止基于COW的 std :: string 实现'的章节和风格样式感兴趣。

Or is this restriction implied, in the sense that it is the combined effect of the new requirements on std::string that precludes a COW based implementation of std::string. In this case, I'd be interested in a chapter and verse style derivation of 'C++11 effectively prohibits COW based std::string implementations'.

推荐答案

这是不允许的,因为根据标准21.4.1 p6,迭代器/引用的无效只允许

It's not allowed, because as per the standard 21.4.1 p6, invalidation of iterators/references is only allowed for


- 作为任何标准库函数的参数,引用
作为非const basic_string作为参数。

— as an argument to any standard library function taking a reference to non-const basic_string as an argument.

- 调用非const
成员函数,除了operator [],at,front,back,begin,rbegin,
end和rend。

— Calling non-const member functions, except operator[], at, front, back, begin, rbegin, end, and rend.

对于COW字符串,调用非const operator [] 将需要进行复制,这是上面段落不允许的。因此,在C ++ 11中使用COW字符串不再是合法的。

For a COW string, calling non-const operator[] would require making a copy (and invalidating references), which is disallowed by the paragraph above. Hence, it's no longer legal to have a COW string in C++11.

这篇关于COW std :: string实现在C ++ 11中的合法性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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