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.
Use a gentle cleanser to spot-clean visibly dirty areas, and always follow up by wiping the area with a dry, absorbent cloth. One simple cleaner that works well with laminate is: 1 part rubbing alcohol + 3 parts water + a squirt of dish soap, applied using a lightly dampened cloth.
To clean your floors with this DIY mixture, combine equal parts olive oil and white vinegar, a few teaspoons of lemon juice, and five parts hot water. Dip your mop or rag into the solution, wring gently, and then wipe down your hardwood floors with this homemade wood floor cleaner that shines.
There is a proposal for c++17
to add the __has_include
macro.
The latest version isn't public yet, but was discussed (approved?) at the last standard meeting: https://www.reddit.com/r/cpp/comments/3q4agc/c17_progress_update_oct_2015/
The previous version: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0061r0.html
Until then I don't think there is a portable way of detecting if a header is available. The best you can do is to check what macro a header defines, bearing in mind that different libraries have different macros and that even in the same libraries the name can change from revision to revision, as it is an internal macro. It's not that bad if you only want to support the major compilers (there are 3) and don't expect your code to be supported 3-5 years from now.
Instead of checking for a header, if it's possible you should check for features. C++11
onward defines a set of feature testing macros
:
http://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros
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