7

I recently learned, that since a few years the library libstdc++ contains vstring (also known as versa_string), which provides the same functionality as std::string, but is apparently more conforming to the C++ standard. I have tried to use vstring as a replacement for std::string, but I have found no easy way to do it.

Is there an easy way to replace std::string with vstring, without changing the libstdc++ sources?

I am fine with replacing all uses of std::string within my code by an alias, as indicated by the following listing. However, the problem with this approach is, that std::string is also used internally in some places, e.g. in std::ostringstream. That means, the statements std::ostringstream os; my::string s = os.str(); no longer works.

namespace my {
#ifdef __GLIBCXX__
    using string = __gnu_cxx::__vstring;
#else
    using string = std::string;
#endif
}
Tim
  • 41,901
  • 18
  • 127
  • 145
nosid
  • 48,932
  • 13
  • 112
  • 139
  • Recent version ? `vstring` was added in 4.1. See http://stackoverflow.com/questions/10463851/what-is-gccs-vstring. – Chnossos Apr 23 '14 at 21:56
  • I seem to remember Herb Sutter saying in some video that gcc4.9 was going to make `std::string` non-ref counted, but I don't see that in the release notes. You can fix the error in the one case you've listed by using `my::string s = os.str().c_str();` instead. There might even be a way to access the underlying streambuf to avoid the intermediate `std::string`, but I'm not sure of that part. – Praetorian Apr 23 '14 at 23:15
  • 3
    @Praetorian: JW said it wouldn't make 4.9 http://stackoverflow.com/questions/21431633/does-g-meets-stdstring-c11-requirements#comment32348898_21431742 – Lightness Races in Orbit Apr 24 '14 at 19:41
  • Indeed, rumours of the COW string's death have been greatly exaggerated ;-) To answer the question, no, there is no way to replace `std::string` in the library. You'll have to wait for GCC 4.10 – Jonathan Wakely Apr 28 '14 at 11:24
  • 1
    @JonathanWakely: I think you could make this an answer. – Matthieu M. May 11 '14 at 17:11

1 Answers1

5

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.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521