What Is Gcc's "Vstring"

What is GCC's vstring?

GCC's vstring is a versatile string class, which was introduced in GCC 4.1's libstdc++ implementation.

It is compatible with std::basic_string, with these additional details:

  • Two base classes are provided:

    • the default one avoids reference counting and is optimized for short strings;
    • the alternate one, still uses it (reference counting, that is) while improving in a few low level areas (e.g., alignment). See vstring_fwd.h for some useful typedefs.
  • Various algorithms have been rewritten (e.g., replace), the code streamlined and simple optimizations added.
  • Option 3 of DR 431 is implemented for both available bases, thus improving the support for stateful allocators.

DR431 is Library Working Group Defect Report 431, with option 3 looking like implementing better allocator support for the class to allow better swapping and other allocator-related operations.

The basic details are from GCC 4.1's release notes, under the Runtime Library section.

edit:

It looks as though the original purpose of this extension was to provide a basis for a C++11 std::string implementation. Paolo Carlini, a GCC/libstdc++ contributor, comments in this GCC Bug Report that <ext/vstring.h> contains a non-reference counted experimental version of the next std::string. Comment dated April 12, 2012:

What we tried to explain is that this sort of issue is well known and, more or
less, affects any reference counted implementation...
That is not the case when reference counting is not used and indeed it will not be
used (per the new C++11 Standard) in a new implementation of std::string which
we are currently showcasing as <ext/vstring.h>
...

How to replace std::string with vstring?

No, there is no way to replace std::string with vstring, it's meant as an alternative string type, not a drop-in replacement for std::string

Since GCC 5.1 the library ships with two implementations of std::string and for any given translation unit you can choose which to use via the _GLIBCXX_USE_CXX11_ABI macro. The two string types have different mangled names, so are not link-compatible.

See Dual ABI for more details.

Is std::string ref-counted in GCC 4.x / C++11?

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.



Related Topics



Leave a reply



Submit