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?
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With