25

I read some GCC bugreport and people there were talking about "vstring". Searching the WEB I came to notice http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.2/vstring_8h.html .

Can someone please elaborate on what it is useful and used for? Why use it instead of std::string?

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 1
    The link you provided mentions right at the top *"This file is a GNU extension to the Standard C++ Library."* Ofcourse, that does not answer your question completely. – Alok Save May 05 '12 at 16:41
  • If memory serves, that is/was an experimental reimplementation of `std::string` with the same interface but different guts. The file has copyright dates in 2005-2007 and nothing since; recommend searching the [`libstdc++` mailing list archive](http://gcc.gnu.org/ml/libstdc++/) in that time period for mentions. – zwol May 05 '12 at 16:46

1 Answers1

23

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

wkl
  • 77,184
  • 16
  • 165
  • 176
  • What's the rationale for inventing it? Will it eventually become the implementation of std::string? – Johannes Schaub - litb May 05 '12 at 16:53
  • 1
    @JohannesSchaub-litb - I believe that was the original purpose of `ext/vstring.h`, that it would provide a non-reference counted string implementation that would eventually become the default (or be the basis) of a new `std::string` implementation for GCC's C++11 implementation. There are various comments in GCC mailing lists and bug reports that seem to point toward that, like this comment: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52938#c5 and this: http://old.nabble.com/Re%3A--RFC--C%2B%2B1x-breaking-the-ABI-in-one-more-place-%3A%28-p28618810.html. That's as much as I know without asking. – wkl May 05 '12 at 16:58
  • 2
    http://gcc.gnu.org/ml/libstdc++/2012-01/msg00064.html implies that the only reason it has not already become the `std::string` implementation is ABI compatibility constraints. – zwol May 05 '12 at 17:19
  • 3
    With the ABI transition in GCC 5 I introduced a new `std::basic_string` implementation, copying most of the code from the SSO versions of vstring. – Jonathan Wakely Sep 21 '15 at 22:55
  • 1
    For those who don't recognize the acronym (like me up until a few seconds ago) - SSO is the "short string optimization" mentioned above. See also [Meaning of acronym SSO in the context of std::string](http://stackoverflow.com/q/10315041/1593077) here on Stackoverflow. – einpoklum Jun 04 '16 at 20:34