Search code examples
c++c++11stdstringlibstdc++

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?


Solution

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