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

- 59,987
- 13
- 123
- 180
-
This is a bit of a generic question. You probably have a particular version in mind, and you can surely check the implementation in the headers... – David Rodríguez - dribeas Sep 20 '12 at 20:14
-
2I believe C++11 introduced requirements that made copy-on-write impossible and thus eliminated the need for a reference count, but I don't remember where I heard that. – Mark Ransom Sep 20 '12 at 20:17
-
`std::string` is reference-counted in GCC-4.7 - independent of the command line options. – nosid Sep 20 '12 at 20:20
-
3@nosid if you can prove that (headers or gcc mailing list discussion etc.) you should write an answer rather than a comment so you can get upvotes ;) – Joseph Garvin Sep 20 '12 at 20:38
-
1I think there's a macro you can set to get non-refcounted strings, something like `FULLY_DYNAMIC_STRINGS` or so. The reason the library maintainers are reluctant to switch is because it would break binary compatibility with code that was compiled earlier. – Kerrek SB Sep 20 '12 at 21:48
-
@KerrekSB I think the "fully dynamic strings" flag in is reference to _small string optimization_ and other optimizations that avoid heap memory. – Drew Dormann Sep 22 '12 at 16:23
-
1@DrewDormann: Hmm, maybe, but GCC never had a small-string optimisation. I'd have to check, though. – Kerrek SB Sep 22 '12 at 22:53
3 Answers
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.

- 449,505
- 63
- 781
- 982
-
12
-
@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. – Nicol Bolas Sep 20 '12 at 20:50
-
7This could work as a reference, or at least as rationale: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html – Cubbi Sep 20 '12 at 22:29
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.

- 1
- 1

- 50,901
- 14
- 124
- 166
-
2This 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](http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01602.html) that obeys the C++11 requirements for
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 Sep 21 '12 at 02:37 -
1Actually, versa_string is a wrapper that could take several representations. One is the refcounted string, another is a [short string optimized](http://stackoverflow.com/questions/1466073/how-is-stdstring-implemented) version. – emsr Sep 21 '12 at 02:51
-
@emsr: Thanks for the info. I found an SO question about it also and added it to my answer. – Jesse Good Sep 21 '12 at 03:00
-
1@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: http://gcc.gnu.org/wiki/Cxx11AbiCompatibility – Joseph Garvin Sep 21 '12 at 18:41
-
@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. – emsr Sep 23 '12 at 04:06
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.

- 59,987
- 13
- 123
- 180
-
2One 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](http://developerblog.redhat.com/2015/02/10/gcc-5-in-fedora/) for more details. Also, for other queries similar to the original post, I put together a useful summary table [here](http://info.prelert.com/blog/cpp-stdstring-implementations#summarytable). – dmr195 Sep 24 '15 at 13:40