Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test whether libstdc++'s version uses a C++11-compliant std::string

I'm writing some C++11 code that makes assumptions about the nature of std::string that are valid, but represent behavior that was changed in C++11. In the earlier days, libstdc++'s basic_string implementation conformed to the 98/03 requirements, but not to the more strict C++11 requirements.

As I understand it, libstdc++ has fixed the issues around basic_string. The problem is that there are many versions of the library that people use which do not implement this fix. And my code may silently fail in many unpleasant ways on them.

I would like to have a static_assert fire if the user attempts to compile my library against those non-conformant versions of libstdc++. How do I detect the version, and equally importantly, which version should I look for?

like image 767
Nicol Bolas Avatar asked Dec 19 '15 22:12

Nicol Bolas


People also ask

What is using std::string?

C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Is std::string the same as string?

There is no functionality difference between string and std::string because they're the same type. That said, there are times where you would prefer std::string over string .

What type is std::string?

The std::string type is the main string datatype in standard C++ since 1998, but it was not always part of C++. From C, C++ inherited the convention of using null-terminated strings that are handled by a pointer to their first element, and a library of functions that manipulate such strings.


1 Answers

The new C++11 compliant std::string was introduced with the new (dual) ABI in GCC 5 (Runtime Library Section of the changelog).

The macro _GLIBCXX_USE_CXX11_ABI decides whether the old or new ABI is being used, so just check it:

#if _GLIBCXX_USE_CXX11_ABI

Of course that's specific to libstdc++ only.

like image 130
AliciaBytes Avatar answered Sep 23 '22 06:09

AliciaBytes