Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace std::string with vstring?

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
}
like image 637
nosid Avatar asked Apr 23 '14 21:04

nosid


People also ask

How do I replace a string with another string in C++?

Replace part of a string with another string in C++ In C++ the replacing is very easy. There is a function called string. replace(). This replace function replaces only the first occurrence of the match.

Is std :: string the same as string?

There is no functionality difference between string and std::string because they're the same type.


1 Answers

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.

like image 58
Jonathan Wakely Avatar answered Oct 02 '22 00:10

Jonathan Wakely