36

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

7
  • 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... Commented Sep 20, 2012 at 20:14
  • 2
    I 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. Commented Sep 20, 2012 at 20:17
  • 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 ;) Commented Sep 20, 2012 at 20:38
  • 1
    I 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. Commented Sep 20, 2012 at 21:48
  • 1
    @DrewDormann: Hmm, maybe, but GCC never had a small-string optimisation. I'd have to check, though. Commented Sep 22, 2012 at 22:53

3 Answers 3

25

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This 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.
Actually, versa_string is a wrapper that could take several representations. One is the refcounted string, another is a short string optimized version.
@emsr: Thanks for the info. I found an SO question about it also and added it to my answer.
@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
@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.
25

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 Comments

You should add a citation to make it authoritative.
@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.
This could work as a reference, or at least as rationale: open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html
14

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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.