Files
2025-10-25 03:02:53 +03:00

213 lines
11 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[module.import]
# 10 Modules [[module]](./#module)
## 10.3 Import declaration [module.import]
[module-import-declaration:](#nt:module-import-declaration "10.3Import declaration[module.import]")
import-keyword [*module-name*](module.unit#nt:module-name "10.1Module units and purviews[module.unit]") [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt ;
import-keyword [*module-partition*](module.unit#nt:module-partition "10.1Module units and purviews[module.unit]") [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt ;
import-keyword [*header-name*](lex.header#nt:header-name "5.6Header names[lex.header]") [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]")opt ;
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L412)
A [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") shall inhabit the global namespace scope[.](#1.sentence-1)
In a module unit, all [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]")*s* and [*export-declaration*](module.interface#nt:export-declaration "10.2Export declaration[module.interface]")*s* exporting[*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]")*s* shall appear before all other [*declaration*](dcl.pre#nt:declaration "9.1Preamble[dcl.pre]")*s* in
the [*declaration-seq*](dcl.pre#nt:declaration-seq "9.1Preamble[dcl.pre]") of the[*translation-unit*](basic.link#nt:translation-unit "6.7Program and linkage[basic.link]") and of the [*private-module-fragment*](module.private.frag#nt:private-module-fragment "10.5Private module fragment[module.private.frag]") (if any)[.](#1.sentence-2)
The optional [*attribute-specifier-seq*](dcl.attr.grammar#nt:attribute-specifier-seq "9.13.1Attribute syntax and semantics[dcl.attr.grammar]") appertains to the [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]")[.](#1.sentence-3)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L425)
A [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") [*imports*](#def:import "10.3Import declaration[module.import]") a set of
translation units determined as described below[.](#2.sentence-1)
[*Note [1](#note-1)*:
Namespace-scope declarations exported by the imported translation units
can be found by name lookup ([[basic.lookup]](basic.lookup "6.5Name lookup"))
in the importing translation unit
and declarations within the imported translation units
become reachable ([[module.reach]](module.reach "10.7Reachability"))
in the importing translation unit
after the import declaration[.](#2.sentence-2)
— *end note*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L438)
A [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") that specifies
a [*module-name*](module.unit#nt:module-name "10.1Module units and purviews[module.unit]") M imports all module interface units of M[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L443)
A [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") that specifies
a [*module-partition*](module.unit#nt:module-partition "10.1Module units and purviews[module.unit]") shall only appear after
the [*module-declaration*](module.unit#nt:module-declaration "10.1Module units and purviews[module.unit]") in a module unit of
some module M[.](#4.sentence-1)
Such a declaration imports the so-named
module partition of M[.](#4.sentence-2)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L451)
A [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") that specifies
a [*header-name*](lex.header#nt:header-name "5.6Header names[lex.header]") H imports
a synthesized [*header unit*](#def:header_unit "10.3Import declaration[module.import]"),
which is a translation unit formed by applying
phases 1 to 7 of translation ([[lex.phases]](lex.phases "5.2Phases of translation"))
to the source file or header nominated by H,
which shall not contain a [*module-declaration*](module.unit#nt:module-declaration "10.1Module units and purviews[module.unit]")[.](#5.sentence-1)
[*Note [2](#note-2)*:
A header unit is a separate translation unit with
an independent set of defined macros[.](#5.sentence-2)
All declarations within a header unit are implicitly
exported ([[module.interface]](module.interface "10.2Export declaration")),
and are attached to the global module ([[module.unit]](module.unit "10.1Module units and purviews"))[.](#5.sentence-3)
— *end note*]
An [*importable header*](#def:header,importable "10.3Import declaration[module.import]") is a member of animplementation-defined
set of headers that
includes all importable C++ library headers ([[headers]](headers "16.4.2.3Headers"))[.](#5.sentence-4)
H shall identify an importable header[.](#5.sentence-5)
Given two such [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]")*s*:
- [(5.1)](#5.1)
if their [*header-name*](lex.header#nt:header-name "5.6Header names[lex.header]")*s* identify
different headers or source files ([[cpp.include]](cpp.include "15.3Source file inclusion")),
they import distinct header units;
- [(5.2)](#5.2)
otherwise, if they appear in the same translation unit,
they import the same header unit;
- [(5.3)](#5.3)
otherwise, it is unspecified whether they import the same header unit[.](#5.sentence-6)
[*Note [3](#note-3)*:
It is therefore possible that multiple copies exist of entities
declared with internal linkage in an importable header[.](#5.3.sentence-2)
— *end note*]
[*Note [4](#note-4)*:
A [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") nominating
a [*header-name*](lex.header#nt:header-name "5.6Header names[lex.header]") 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]](cpp.import "15.6Header unit importation")[.](#5.sentence-7)
Any other [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") does not make macros visible[.](#5.sentence-8)
— *end note*]
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L497)
A declaration of a name with internal linkage is
permitted within a header unit despite all
declarations being implicitly exported ([[module.interface]](module.interface "10.2Export declaration"))[.](#6.sentence-1)
[*Note [5](#note-5)*:
A definition that appears in multiple translation units
cannot in general refer to such names ([[basic.def.odr]](basic.def.odr "6.3One-definition rule"))[.](#6.sentence-2)
— *end note*]
A header unit shall not contain
a definition of a non-inline function or variable
whose name has external linkage[.](#6.sentence-3)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L509)
When a [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") imports
a translation unit T, it also imports
all translation units imported by
exported [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]")*s* in T; such translation units are
said to be [*exported*](#def:module,exported "10.3Import declaration[module.import]") by T[.](#7.sentence-1)
Additionally, when a [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") in 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*](#nt:module-import-declaration "10.3Import declaration[module.import]")*s* in the module unit purview of U[.](#7.sentence-2)[85](#footnote-85 "This is consistent with the lookup rules for imported names ([basic.lookup]).")
These rules can in turn lead to the importation of yet more
translation units[.](#7.sentence-3)
[*Note [6](#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]](lex.phases "5.2Phases of translation"))[.](#7.sentence-4)
Macros can be made available by directly importing header units
as described in [[cpp.import]](cpp.import "15.6Header unit importation")[.](#7.sentence-5)
— *end note*]
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L535)
A module implementation unit shall not be exported[.](#8.sentence-1)
[*Example [1](#example-1)*:
Translation unit #1:module M:Part;
Translation unit #2:export module M;export import :Part; // error: exported partition :Part is an implementation unit — *end example*]
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L548)
A module implementation unit of a module M that is not a module partition
shall not contain a [*module-import-declaration*](#nt:module-import-declaration "10.3Import declaration[module.import]") nominating M[.](#9.sentence-1)
[*Example [2](#example-2)*: module M;import M; // error: cannot import M in its own unit — *end example*]
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/modules.tex#L560)
A translation unit has an [*interface dependency*](#def:interface_dependency "10.3Import declaration[module.import]") on a translation unit U if it contains a declaration (possibly a [*module-declaration*](module.unit#nt:module-declaration "10.1Module units and purviews[module.unit]"))
that imports U or if it has
an interface dependency on a translation unit that has an interface dependency on U[.](#10.sentence-1)
A translation unit shall not have an interface dependency on itself[.](#10.sentence-2)
[*Example [3](#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; // error: cyclic interface dependency M3 →M1 →M2 →M3 — *end example*]
[85)](#footnote-85)[85)](#footnoteref-85)
This is consistent
with the lookup rules for imported names ([[basic.lookup]](basic.lookup "6.5Name lookup"))[.](#footnote-85.sentence-1)