在 GCC 4.x/C++11 中是否计算了 std::string 引用?
当使用带有 -std=c++0x
或 -std=c++11
的 gcc 4 时 std::string
引用计数代码>?
Is std::string
reference-counted when using gcc 4 with -std=c++0x
or -std=c++11
?
推荐答案
看libstdc++ 文档 我发现(有关更多信息,请参阅链接):
Looking at libstdc++ documentation I find (see the link for more info):
一个字符串看起来像这样:
A string looks like this:
[_Rep]
_M_length
[basic_string<char>] _M_capacity
_M_dataplus _M_refcount
_M_p ----------------> unnamed array of char_type
所以,是的,它是参考计数的.另外,来自这里的讨论:
So, yes it is ref counted. Also, from the discussion here:
是的,std::string 在某些时候会变成非引用计数,但是作为非引用计数的字符串在 C++98 中也是有效的,一个选项是切换到非引用计数的字符串-std=c++98 和 -std=c++11 模式.我不是说这就是会发生的事情,但它可能会发生.
Yes, std::string will be made non-reference counting at some point, but as a non-reference-counted string is valid in C++98 as well, one option would be to switch to a non-ref-counted string for both -std=c++98 and -std=c++11 modes. I'm not saying that's what will happen, but it could be.
所以,似乎有计划将其更改为符合标准(但我不知道进展如何).
So, it seems there are plans to change it to be conforming (I don't know how the progress is going though).
更新正如 emsr 在评论中指出的那样,目前有一个非引用计数扩展名为 vstring.h
,这似乎是它没有取代 std::string
的唯一原因> 是因为 ABI 兼容性.此处有一个 SO 问题.
Update
As emsr points out in the comments, there is currently a non-reference counted extension called vstring.h
, and it seems the only reason it hasn't replaced std::string
is because of ABI compatibility. There is an SO question about it here.
相关文章