Is std::string refcounted in GCC 4.x / C++11?

拈花ヽ惹草 提交于 2019-11-26 06:44:25

问题


Is std::string reference-counted when using gcc 4 with -std=c++0x or -std=c++11?


回答1:


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:

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).

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.




回答2:


C++11 added specific language forbidding std::string from being reference counted. So if it is, then it's a pretty significant failing in GCC's C++11 standard library.




回答3:


Adding some useful information that post-dates this question.

std::string will no longer be reference-counted with the release of GCC 5, to address this C++11 requirement.

From https://gcc.gnu.org/gcc-5/changes.html

A new implementation of std::string is enabled by default, using the small string optimization instead of copy-on-write reference counting.



来源:https://stackoverflow.com/questions/12520192/is-stdstring-refcounted-in-gcc-4-x-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!