Search code examples
c++c++11boostc-preprocessortr1

Determine compile-time existence of include files in C++


I'm trying to write some portable C++ library code that will initially rely on Boost.Regex, and then move to TR1 as compilers support it, and eventually to the C++0x specification after things get moved from the std::tr1 namespace to std. Here is some pseudo-code for what I'd like to do with the preprocessor:

if( exists(regex) )    // check if I can #include <regex>
{
    #include <regex>    // per TR1
    if( is_namespace(std::tr1) )   // are we on TR1 or C++0x?
    {
        using std::tr1::regex;
    } else
    {
        using std::regex;
    }
} else    // fall-back to boost
{
    #include <boost/regex.hpp>
    using boost::regex;
}

Granted, all of that would need to be in preprocessor directives, but if I knew how to accomplish that I wouldn't be asking here. :)


Solution

  • You can't do it without relying on a third party thing before preprocessing. Generally, things like autoconf can be used to accomplish this.

    They work by generating another header file with #define directives that indicate existence of headers/libraries you want to use.