First of all, I've read this question: Is there a way to detect portably that a standard header is included using macros?
And what I want to know is: How safe is it to use #ifdef
for the task of detecting if a c++ std header is included, like in the code below:
namespace overwrite
{
using byte = unsigned char;
template <bool safeMode = true, typename generic>
void withZeros( generic *toBeOverwriten, size_t length = 1 )
{
// do stuff
}
#ifdef _GLIBCXX_RANDOM // found this macro inside <random>
template <bool safeMode = true, typename generic>
void withRandomData( generic *toBeOverwriten, byte min = 0, byte max = 255 )
{
// do stuff expecting <random> to be included
}
#endif
}
...so that I could not just overload some std function as "worse match" as proposed in the answer to the mentioned question, but also compile or not a whole function/section of My header file, depending on the inclusion of some std header.
Is this way not safe at all, as I suspect? If so, are there any other ways to detect this in order to do what I want to?
Regarding to "Why the heck don't I just include the header"...
The code I give as an example of what I'm trying to do is Just An Example. I had other things in my mind too and just wanted to know if there was another way to check for the inclusion of headers without checking macros you expect to be defined inside those. Then I remembered of this real situation where I asked myself about this and I started by asking what I'm asking... since, in this given case, I don't want to include a lot of code (<random>
is longer than 20 or 30 LOC) just to "sustain" a single function of my header.