Skip to content

Consider adding __has_include #12

@bonachea

Description

@bonachea

__has_include was added to CPP in C++17 and C23.

In a nutshell it provides a non-fatal probe of whether some proposed arguments for a given #include directive will succeed. The __has_include(file-name) expression evaluates to 1 if the search for the source file succeeds, and to ​0​ if the search fails. This enables programmatic fallback across potential header file names and more user-friendly error reporting for missing dependencies, all from within the preprocessor.

Here is a C++ example (modified from cppreference):

#if __has_include(<optional>)
#  include <optional>
#  define has_optional 1
   template<class T> using optional_t = std::optional<T>;
#elif __has_include(<experimental/optional>)
#  include <experimental/optional>
#  define has_optional -1
   template<class T> using optional_t = std::experimental::optional<T>;
#else
#  error No recognized header file found for the "optional" library. For more information on this error see ...
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions