Source file inclusion
Includes other source file into current source file at the line immediately after the directive.
Syntax
#include < h-char-sequence > new-line
|
(1) | ||||||||
#include " q-char-sequence " new-line
|
(2) | ||||||||
#include pp-tokens new-line
|
(3) | ||||||||
__has_include
(
"
q-char-sequence
"
)
__has_include
(
<
h-char-sequence
>
)
|
(4) | (since C++17) | |||||||
__has_include
(
string-literal
)
__has_include
(
<
h-pp-tokens
>
)
|
(5) | (since C++17) | |||||||
new-line | - | The new-line character |
h-char-sequence | - | A sequence of one or more h-chars, where the appearance of any of the following is conditionally-supported with implementation-defined semantics:
|
h-char | - | Any member of the source character set(until C++23) translation character set (since C++23) except new-line and > |
q-char-sequence | - | A sequence of one or more q-chars, where the appearance of any of the following is conditionally-supported with implementation-defined semantics:
|
q-char | - | Any member of the source character set(until C++23) translation character set (since C++23) except new-line and " |
pp-tokens | - | A sequence of one or more preprocessing tokens |
string-literal | - | A string literal |
h-pp-tokens | - | A sequence of one or more preprocessing tokens except > |
Explanation
include
#include
directive, the program is ill-formed. The __has_include
expression evaluates to 1 if the search for the source file succeeds, and to 0
If the header identified by the header-name (i.e.,
|
(since C++20) |
__has_include
can be expanded in the expression of #if and #elif. It is treated as a defined macro by #ifdef, #ifndef
, #elifdef, #elifndef
(since C++23)
and defined
Notes
Typical implementations search only standard include directories for syntax (1)
The intent of syntax (2) is to search for the files that are not controlled by the implementation. Typical implementations first search the directory where the current file resides then falls back to (1)
When a file is included, it is processed by translation phases 1-4, which may include, recursively, expansion of the nested #include
directives, up to an implementation-defined nesting limit. To avoid repeated inclusion of the same file and endless recursion when a file includes itself, perhaps transitively, header guards
#ifndef FOO_H_INCLUDED /* any name uniquely mapped to file name */ #define FOO_H_INCLUDED // contents of the file are here #endif
Many compilers also implement the non-standard pragma #pragma once
A sequence of characters that resembles an escape sequence in q-char-sequence or h-char-sequence
A __has_include
result of
1
only means that a header or source file with the specified name exists. It does not mean that the header or source file, when included, would not cause an error or would contain anything useful. For example, on a C++ implementation that supports both C++14 and C++17 modes (and provides __has_include in its C++14 mode as a conforming extension), __has_include(<optional>) may be 1 in C++14 mode, but actually #include <optional>
Example
#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 #define has_optional 0 template<class V> class optional_t { V v{}; bool has{}; public: optional_t() = default; optional_t(V&& v) : v(v), has{true} {} V value_or(V&& alt) const& { return has ? v : alt; } // etc. }; #endif #include <iostream> int main() { if (has_optional > 0) std::cout << "<optional> is present\n"; else if (has_optional < 0) std::cout << "<experimental/optional> is present\n"; else std::cout << "<optional> is not present\n"; optional_t<int> op; std::cout << "op = " << op.value_or(-1) << '\n'; op = 42; std::cout << "op = " << op.value_or(-1) << '\n'; }
Output:
<optional> is present op = -1 op = 42
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 787 | C++98 | the behavior is undefined if an escape sequence is resembled in q-char-sequence or h-char-sequence |
it is conditionally-supported |
See also
A list of C++ Standard Library header files | |
C documentation for Source file inclusion
|