[cpp.import] (original) (raw)

15 Preprocessing directives [cpp]

15.5 Header unit importation [cpp.import]

pp-import: export import header-name pp-tokens ; new-line export import header-name-tokens pp-tokens ; new-line export import pp-tokens ; new-line

A pp-import shall not appear in a context where importor (if it is the first token of the pp-import) exportis an identifier defined as an object-like macro.

The preprocessing tokens after the import preprocessing token in the import control-lineare processed just as in normal text (i.e., each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens).

An import directive matching the first two forms of a pp-importinstructs the preprocessor to import macros from the header unit ([module.import]) denoted by the header-name.

The point of macro import for the first two forms of pp-import is immediately after the new-line terminating the pp-import.

The last form of pp-import is only considered if the first two forms did not match.

If a pp-import is produced by source file inclusion (including by the rewrite produced when a #include directive names an importable header) while processing the group of a module-file, the program is ill-formed.

In all three forms of pp-import, the import and export (if it exists) preprocessing tokens are replaced by the import-keyword andexport-keyword preprocessing tokens respectively.

[ Note

:

This makes the line no longer a directive so it is not removed at the end of phase 4.

end note

]

[ Note

:

This ensures that imports are treated consistently by the preprocessor and later phases of translation.

end note

]

Each #define directive encountered when preprocessing each translation unit in a program results in a distinctmacro definition.

[ Note

:

A predefined macro name ([cpp.predefined]) is not introduced by a #define directive.

Implementations providing mechanisms to predefine additional macros are encouraged to not treat them as being introduced by a #define directive.

end note

]

Importing macros from a header unit makes macro definitions from a translation unit visible in other translation units.

Each macro definition has at most one point of definition in each translation unit and at most one point of undefinition, as follows:

A macro directive is active at a source location if it has a point of definition in that translation unit preceding the location, and does not have a point of undefinition in that translation unit preceding the location.

If a macro would be replaced or redefined, and multiple macro definitions are active for that macro name, the active macro definitions shall all be valid redefinitions of the same macro ([cpp.replace]).

[ Note

:

The relative order of pp-imports has no bearing on whether a particular macro definition is active.

end note

]

[ Example

:

Importable header "a.h":

#define X 123
#define Y 45
#define Z a
#undef X

Importable header "b.h":

import "a.h";
#define X 456
#define Y 6

Importable header "c.h":

#define Y 45
#define Z c

Importable header "d.h":

import "a.h";
import "c.h";
int a = Y;
int c = Z;

end example

]