Is std::string ref-counted in GCC 4.x / C++11?
Solution 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.
Solution 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.
Solution 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.
Drew Dormann
Updated on June 03, 2022Comments
-
Drew Dormann almost 2 years
Is
std::string
reference-counted when using gcc 4 with-std=c++0x
or-std=c++11
? -
Joseph Garvin over 11 yearsYou should add a citation to make it authoritative.
-
Nicol Bolas over 11 years@JosephGarvin: I can cite some language that makes it impossible for C++11 to use reference counting, but I don't have the C++98/03 spec to cite the original language that made reference counting possible. I don't know specifically what changed; I just know what rules make it currently impossible.
-
Cubbi over 11 yearsThis could work as a reference, or at least as rationale: open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html
-
emsr over 11 yearsThis is a known defect among libstdc++ maintainers. Fixing it would involve breaking the ABI and causing a lot of trouble. As an extension we have the versa string link that obeys the C++11 requirements for <string> in that it doesn't refcount. At some point there will be a point when we decide to break the ABI with this and several other C++11 changes.
-
emsr over 11 yearsActually, versa_string is a wrapper that could take several representations. One is the refcounted string, another is a short string optimized version.
-
Jesse Good over 11 years@emsr: Thanks for the info. I found an SO question about it also and added it to my answer.
-
Joseph Garvin over 11 years@emsr: Does this mean they reverted all the ABI breaking changes when in C++11 mode? The GCC wiki still says that there's breakage: gcc.gnu.org/wiki/Cxx11AbiCompatibility
-
emsr over 11 years@JosephGarvin: I'm pretty sure the List::_M_size was reverted but I'm sure much of the remaining ABI breakage is there. I would follow the instructions on checking in the Wiki.
-
dmr195 over 8 yearsOne complication is that although the gcc developers made the new std::string implementation the default in g++ 5.1, Fedora 22 (one of the first Linux distributions to include g++ 5.1) chose to set the default back to reference-counted strings. To get a non-reference-counted string on Fedora 22 build with -D_GLIBCXX_USE_CXX11_ABI=1. See here for more details. Also, for other queries similar to the original post, I put together a useful summary table here.