6

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.

7
  • 1
    Just include the header?
    – Daniel
    Oct 31, 2015 at 18:38
  • BTW, why don't you simply #include random? Oct 31, 2015 at 18:40
  • I could just include the header, of course, but I want to know if it's possible to do what I've proposed... Oct 31, 2015 at 18:41
  • Can you clarify what problem it would solve? Unless that's what you want to truly know.
    – edmz
    Oct 31, 2015 at 18:47
  • 1
    The case of std headers was one of some I thought about. But the only real problems I see related to this is 1) that I have a header defining a red-black tree and another defining a shuffle function and I wanted to include a member function to my red black tree, which returns a shuffled list of its elements, IF shuffle function is there too (in other worlds: if shuffle.hpp is included...) and 2) also, I just wanted to have the overwrite::withRandomData if <random> is already included because I do not want to include a lot of code just to "sustain" this single function. Oct 31, 2015 at 18:59

1 Answer 1

3

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

2
  • Very interesting links, specially this one about feature testing... But __has_include looks kinda far away from now :( Oct 31, 2015 at 19:11
  • __has_include is in C++17, but note that it doesn't exactly solve the OP's problem: there's a difference between checking whether a header can be included and checking whether it has been included. __has_include does the former, whereas the OP seems to be asking about the latter.
    – deltacrux
    Oct 5, 2021 at 6:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.