[module.import] (original) (raw)
10 Modules [module]
10.3 Import declaration [module.import]
A module-import-declarationshall inhabit the global namespace scope.
A module-import-declaration imports a set of translation units determined as described below.
[Note 1:
Namespace-scope declarations exported by the imported translation units can be found by name lookup ([basic.lookup]) in the importing translation unit and declarations within the imported translation units become reachable ([module.reach]) in the importing translation unit after the import declaration.
— _end note_]
A module-import-declaration that specifies a module-name Mimports all module interface units of M.
Such a declaration imports the so-named module partition of M.
A module-import-declaration that specifies a H imports a synthesized , which is a translation unit formed by applying phases 1 to 7 of translation ([lex.phases]) to the source file or header nominated by H, which shall not contain a module-declaration.
[Note 2:
A header unit is a separate translation unit with an independent set of defined macros.
All declarations within a header unit are implicitly exported ([module.interface]), and are attached to the global module ([module.unit]).
— _end note_]
An is a member of animplementation-defined set of headers that includes all importable C++ library headers ([headers]).
H shall identify an importable header.
Given two such module-import-declarations:
- if their s identify different headers or source files ([cpp.include]), they import distinct header units;
- otherwise, if they appear in the same translation unit, they import the same header unit;
- otherwise, it is unspecified whether they import the same header unit.
[Note 3:
It is therefore possible that multiple copies exist of entities declared with internal linkage in an importable header.
— _end note_]
[Note 4:
A module-import-declaration nominating a is also recognized by the preprocessor, and results in macros defined at the end of phase 4 of translation of the header unit being made visible as described in [cpp.import].
Any other module-import-declarationdoes not make macros visible.
— _end note_]
A declaration of a name with internal linkage is permitted within a header unit despite all declarations being implicitly exported ([module.interface]).
[Note 5:
A definition that appears in multiple translation units cannot in general refer to such names ([basic.def.odr]).
— _end note_]
A header unit shall not contain a definition of a non-inline function or variable whose name has external linkage.
When a module-import-declaration imports a translation unit T, it also imports all translation units imported by exported module-import-declaration_s_in T; such translation units are said to be exported by T.
Additionally, when a module-import-declarationin a module unit of some module M imports another module unit U of M, it also imports all translation units imported by non-exported module-import-declaration_s_in the module unit purview of U.85
These rules can in turn lead to the importation of yet more translation units.
[Note 6:
Such indirect importation does not make macros available, because a translation unit is a sequence of tokens in translation phase 7 ([lex.phases]).
Macros can be made available by directly importing header units as described in [cpp.import].
— _end note_]
A module implementation unit shall not be exported.
[Example 1:
Translation unit #1:module M:Part;
Translation unit #2:export module M;export import :Part; — _end example_]
A module implementation unit of a module Mthat is not a module partition shall not contain a module-import-declarationnominating M.
[Example 2: module M;import M; — _end example_]
A translation unit has an interface dependency on a translation unit Uif it contains a declaration (possibly a module-declaration) that imports U or if it has an interface dependency on a translation unit that has an interface dependency on U.
A translation unit shall not have an interface dependency on itself.
[Example 3:
Interface unit of M1:export module M1;import M2;
Interface unit of M2:export module M2;import M3;
Interface unit of M3:export module M3;import M1; — _end example_]