I want to supply some inline convenience functions using std::string
in my header alongside with the library functions which use const char *
, but I do not want to include <string>
. I want to check with #ifdef
if <string>
is included and provide the convenience functions if this is the case.
Question: Are the names of header guards in STL headers the same for all STL implementations? In Visual Studio 2010, the header guard of <string>
is _STRING_
.
This is a BAD IDEA™.
Generally, you cannot and shouldn't rely on an implementation detail of a compiler / library1. On top on that, as stated by Fire Lancer in the comments, "having includes have different effects based on order will confuse people" and adds to the wtf/line of your library.
What you could (should?) do is document a macro for the user to define in order to enable your std::string
functions:
#ifdef MY_LIBRARY_ENABLE_STRING_FUNCTIONS
void print(std::string message);
#endif // MY_LIBRARY_ENABLE_STRING_FUNCTIONS
If the user wants them, they'll have to:
#define MY_LIBRARY_ENABLE_STRING_FUNCTIONS
#include <my_library>
1) C++17 has the __has_include(<filename>)
macro (credit to acraig5075 for learning this to me) which doesn't help as it returns whether an include is available and not if it has been included.
The most reliable way to check would probably be using sfinae technique.
That being said, DON'T DO THIS, everything screams anti-pattern when you want to differentiate on whether a header has been included or not, or the order of includes.
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