且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

g ++满足std :: string C ++ 11要求

更新时间:2023-11-14 16:15:22

a href =http://***.com/q/12199710/560648>禁止COW 。但GCC 尚未更新其实施,据称为。一个新的实现,最终设计为取代 std :: string 实现,可以找到 ext / vstring.h

You're right that COW is disallowed. But GCC hasn't updated its implementation yet, allegedly due to ABI constraints. A new implementation, designed eventually to supplant the std::string implementation, can be found as ext/vstring.h.

中的错误libstdc ++ std :: string ,虽然不是这个,不会使它进入GCC 4.9; Jonathan在bug上指出它迄今为止只修复了 vstring

尽管如此,放弃 const ness然后突变常常是一个坏主意:虽然你是正确的,这应该在实践中是一个完全C ++ 11兼容的字符串实现安全,你在做假设,这个问题证明,你不能总是依靠这些假设来持有。因此,虽然您的代码示例可能是受欢迎的,但它在劣质代码中很流行,现在不应该写入。而且,当然,在C ++ 03中写的是无能为力的。

Despite all this, casting away constness then mutating is pretty much always a bad idea: though you're correct that this should in practice be safe with a fully C++11-compliant string implementation, you're making assumptions and this very problem proves that you cannot always rely on those assumptions to hold. So, while your code example may be "popular", it's popular in poor code, and shouldn't be written even now. And, of course, writing that in C++03 is flat-out incompetence!