From Jason Turner

[basic.link]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpyim821ty/{from.md → to.md} +234 -88
tmp/tmpyim821ty/{from.md → to.md} RENAMED
@@ -1,79 +1,105 @@
1
  ## Program and linkage <a id="basic.link">[[basic.link]]</a>
2
 
3
- A *program* consists of one or more *translation units* (Clause 
4
- [[lex]]) linked together. A translation unit consists of a sequence of
5
  declarations.
6
 
7
  ``` bnf
8
  translation-unit:
9
  declaration-seqₒₚₜ
 
10
  ```
11
 
12
  A name is said to have *linkage* when it might denote the same object,
13
  reference, function, type, template, namespace or value as a name
14
  introduced by a declaration in another scope:
15
 
16
  - When a name has *external linkage*, the entity it denotes can be
17
  referred to by names from scopes of other translation units or from
18
  other scopes of the same translation unit.
 
 
 
 
19
  - When a name has *internal linkage*, the entity it denotes can be
20
  referred to by names from other scopes in the same translation unit.
21
  - When a name has *no linkage*, the entity it denotes cannot be referred
22
  to by names from other scopes.
23
 
24
- A name having namespace scope ([[basic.scope.namespace]]) has internal
25
  linkage if it is the name of
26
 
27
- - a variable, function or function template that is explicitly declared
28
- `static`; or,
29
- - a non-inline variable of non-volatile const-qualified type that is
30
- neither explicitly declared `extern` nor previously declared to have
31
- external linkage; or
 
 
32
  - a data member of an anonymous union.
33
 
 
 
 
 
34
  An unnamed namespace or a namespace declared directly or indirectly
35
  within an unnamed namespace has internal linkage. All other namespaces
36
  have external linkage. A name having namespace scope that has not been
37
- given internal linkage above has the same linkage as the enclosing
38
- namespace if it is the name of
39
 
40
  - a variable; or
41
  - a function; or
42
- - a named class (Clause  [[class]]), or an unnamed class defined in a
43
- typedef declaration in which the class has the typedef name for
44
- linkage purposes ([[dcl.typedef]]); or
45
- - a named enumeration ([[dcl.enum]]), or an unnamed enumeration defined
46
- in a typedef declaration in which the enumeration has the typedef name
47
- for linkage purposes ([[dcl.typedef]]); or
48
- - a template.
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  In addition, a member function, static data member, a named class or
51
  enumeration of class scope, or an unnamed class or enumeration defined
52
  in a class-scope typedef declaration such that the class or enumeration
53
- has the typedef name for linkage purposes ([[dcl.typedef]]), has the
54
- same linkage, if any, as the name of the class of which it is a member.
55
 
56
  The name of a function declared in block scope and the name of a
57
  variable declared by a block scope `extern` declaration have linkage. If
58
- there is a visible declaration of an entity with linkage having the same
59
- name and type, ignoring entities declared outside the innermost
60
- enclosing namespace scope, the block scope declaration declares that
61
- same entity and receives the linkage of the previous declaration. If
62
- there is more than one such matching entity, the program is ill-formed.
 
 
 
63
  Otherwise, if no matching entity is found, the block scope entity
64
  receives external linkage. If, within a translation unit, the same
65
  entity is declared with both internal and external linkage, the program
66
  is ill-formed.
67
 
68
  [*Example 1*:
69
 
70
  ``` cpp
71
  static void f();
 
72
  static int i = 0; // #1
73
  void g() {
74
  extern void f(); // internal linkage
 
75
  int i; // #2: i has no linkage
76
  {
77
  extern void f(); // internal linkage
78
  extern int i; // #3: external linkage, ill-formed
79
  }
@@ -112,82 +138,202 @@ void q() { ... } // some other, unrelated q
112
  ```
113
 
114
  — *end example*]
115
 
116
  Names not covered by these rules have no linkage. Moreover, except as
117
- noted, a name declared at block scope ([[basic.scope.block]]) has no
118
- linkage. A type is said to have linkage if and only if:
119
 
120
- - it is a class or enumeration type that is named (or has a name for
121
- linkage purposes ([[dcl.typedef]])) and the name has linkage; or
122
- - it is an unnamed class or unnamed enumeration that is a member of a
123
- class with linkage; or
124
- - it is a specialization of a class template (Clause  [[temp]])[^10]; or
125
- - it is a fundamental type ([[basic.fundamental]]); or
126
- - it is a compound type ([[basic.compound]]) other than a class or
127
- enumeration, compounded exclusively from types that have linkage; or
128
- - it is a cv-qualified ([[basic.type.qualifier]]) version of a type
129
- that has linkage.
130
-
131
- A type without linkage shall not be used as the type of a variable or
132
- function with external linkage unless
133
-
134
- - the entity has C language linkage ([[dcl.link]]), or
135
- - the entity is declared within an unnamed namespace (
136
- [[namespace.def]]), or
137
- - the entity is not odr-used ([[basic.def.odr]]) or is defined in the
138
- same translation unit.
139
-
140
- [*Note 1*: In other words, a type without linkage contains a class or
141
- enumeration that cannot be named outside its translation unit. An entity
142
- with external linkage declared using such a type could not correspond to
143
- any other entity in another translation unit of the program and thus
144
- must be defined in the translation unit if it is odr-used. Also note
145
- that classes with linkage may contain members whose types do not have
146
- linkage, and that typedef names are ignored in the determination of
147
- whether a type has linkage. — *end note*]
148
-
149
- [*Example 3*:
150
-
151
- ``` cpp
152
- template <class T> struct B {
153
- void g(T) { }
154
- void h(T);
155
- friend void i(B, T) { }
156
- };
157
-
158
- void f() {
159
- struct A { int x; }; // no linkage
160
- A a = { 1 };
161
- B<A> ba; // declares B<A>::g(A) and B<A>::h(A)
162
- ba.g(a); // OK
163
- ba.h(a); // error: B<A>::h(A) not defined in the translation unit
164
- i(ba, a); // OK
165
- }
166
- ```
167
-
168
- — *end example*]
169
-
170
- Two names that are the same (Clause  [[basic]]) and that are declared in
171
  different scopes shall denote the same variable, function, type,
172
  template or namespace if
173
 
174
- - both names have external linkage or else both names have internal
175
- linkage and are declared in the same translation unit; and
 
176
  - both names refer to members of the same namespace or to members, not
177
  by inheritance, of the same class; and
178
- - when both names denote functions, the parameter-type-lists of the
179
- functions ([[dcl.fct]]) are identical; and
180
- - when both names denote function templates, the signatures (
181
- [[temp.over.link]]) are the same.
182
 
183
- After all adjustments of types (during which typedefs ([[dcl.typedef]])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  are replaced by their definitions), the types specified by all
185
  declarations referring to a given variable or function shall be
186
  identical, except that declarations for an array object can specify
187
  array types that differ by the presence or absence of a major array
188
- bound ([[dcl.array]]). A violation of this rule on type identity does
189
- not require a diagnostic.
190
 
191
- [*Note 2*: Linkage to non-C++declarations can be achieved using a
192
- *linkage-specification* ([[dcl.link]]). — *end note*]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
 
1
  ## Program and linkage <a id="basic.link">[[basic.link]]</a>
2
 
3
+ A *program* consists of one or more translation units [[lex.separate]]
4
+ linked together. A translation unit consists of a sequence of
5
  declarations.
6
 
7
  ``` bnf
8
  translation-unit:
9
  declaration-seqₒₚₜ
10
+ global-module-fragmentₒₚₜ module-declaration declaration-seqₒₚₜ private-module-fragmentₒₚₜ
11
  ```
12
 
13
  A name is said to have *linkage* when it might denote the same object,
14
  reference, function, type, template, namespace or value as a name
15
  introduced by a declaration in another scope:
16
 
17
  - When a name has *external linkage*, the entity it denotes can be
18
  referred to by names from scopes of other translation units or from
19
  other scopes of the same translation unit.
20
+ - When a name has *module linkage*, the entity it denotes can be
21
+ referred to by names from other scopes of the same module unit
22
+ [[module.unit]] or from scopes of other module units of that same
23
+ module.
24
  - When a name has *internal linkage*, the entity it denotes can be
25
  referred to by names from other scopes in the same translation unit.
26
  - When a name has *no linkage*, the entity it denotes cannot be referred
27
  to by names from other scopes.
28
 
29
+ A name having namespace scope [[basic.scope.namespace]] has internal
30
  linkage if it is the name of
31
 
32
+ - a variable, variable template, function, or function template that is
33
+ explicitly declared `static`; or
34
+ - a non-template variable of non-volatile const-qualified type, unless
35
+ - it is explicitly declared `extern`, or
36
+ - it is inline or exported, or
37
+ - it was previously declared and the prior declaration did not have
38
+ internal linkage; or
39
  - a data member of an anonymous union.
40
 
41
+ [*Note 1*: An instantiated variable template that has const-qualified
42
+ type can have external or module linkage, even if not declared
43
+ `extern`. — *end note*]
44
+
45
  An unnamed namespace or a namespace declared directly or indirectly
46
  within an unnamed namespace has internal linkage. All other namespaces
47
  have external linkage. A name having namespace scope that has not been
48
+ given internal linkage above and that is the name of
 
49
 
50
  - a variable; or
51
  - a function; or
52
+ - a named class [[class.pre]], or an unnamed class defined in a typedef
53
+ declaration in which the class has the typedef name for linkage
54
+ purposes [[dcl.typedef]]; or
55
+ - a named enumeration [[dcl.enum]], or an unnamed enumeration defined in
56
+ a typedef declaration in which the enumeration has the typedef name
57
+ for linkage purposes [[dcl.typedef]]; or
58
+ - an unnamed enumeration that has an enumerator as a name for linkage
59
+ purposes [[dcl.enum]]; or
60
+ - a template
61
+
62
+ has its linkage determined as follows:
63
+
64
+ - if the enclosing namespace has internal linkage, the name has internal
65
+ linkage;
66
+ - otherwise, if the declaration of the name is attached to a named
67
+ module [[module.unit]] and is not exported [[module.interface]], the
68
+ name has module linkage;
69
+ - otherwise, the name has external linkage.
70
 
71
  In addition, a member function, static data member, a named class or
72
  enumeration of class scope, or an unnamed class or enumeration defined
73
  in a class-scope typedef declaration such that the class or enumeration
74
+ has the typedef name for linkage purposes [[dcl.typedef]], has the same
75
+ linkage, if any, as the name of the class of which it is a member.
76
 
77
  The name of a function declared in block scope and the name of a
78
  variable declared by a block scope `extern` declaration have linkage. If
79
+ such a declaration is attached to a named module, the program is
80
+ ill-formed. If there is a visible declaration of an entity with linkage,
81
+ ignoring entities declared outside the innermost enclosing namespace
82
+ scope, such that the block scope declaration would be a (possibly
83
+ ill-formed) redeclaration if the two declarations appeared in the same
84
+ declarative region, the block scope declaration declares that same
85
+ entity and receives the linkage of the previous declaration. If there is
86
+ more than one such matching entity, the program is ill-formed.
87
  Otherwise, if no matching entity is found, the block scope entity
88
  receives external linkage. If, within a translation unit, the same
89
  entity is declared with both internal and external linkage, the program
90
  is ill-formed.
91
 
92
  [*Example 1*:
93
 
94
  ``` cpp
95
  static void f();
96
+ extern "C" void h();
97
  static int i = 0; // #1
98
  void g() {
99
  extern void f(); // internal linkage
100
+ extern void h(); // C language linkage
101
  int i; // #2: i has no linkage
102
  {
103
  extern void f(); // internal linkage
104
  extern int i; // #3: external linkage, ill-formed
105
  }
 
138
  ```
139
 
140
  — *end example*]
141
 
142
  Names not covered by these rules have no linkage. Moreover, except as
143
+ noted, a name declared at block scope [[basic.scope.block]] has no
144
+ linkage.
145
 
146
+ Two names that are the same [[basic.pre]] and that are declared in
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  different scopes shall denote the same variable, function, type,
148
  template or namespace if
149
 
150
+ - both names have external or module linkage and are declared in
151
+ declarations attached to the same module, or else both names have
152
+ internal linkage and are declared in the same translation unit; and
153
  - both names refer to members of the same namespace or to members, not
154
  by inheritance, of the same class; and
155
+ - when both names denote functions or function templates, the
156
+ signatures ([[defns.signature]], [[defns.signature.templ]]) are the
157
+ same.
 
158
 
159
+ If multiple declarations of the same name with external linkage would
160
+ declare the same entity except that they are attached to different
161
+ modules, the program is ill-formed; no diagnostic is required.
162
+
163
+ [*Note 2*: *using-declaration*s, typedef declarations, and
164
+ *alias-declaration*s do not declare entities, but merely introduce
165
+ synonyms. Similarly, *using-directive*s do not declare entities.
166
+ Enumerators do not have linkage, but may serve as the name of an
167
+ enumeration with linkage [[dcl.enum]]. — *end note*]
168
+
169
+ If a declaration would redeclare a reachable declaration attached to a
170
+ different module, the program is ill-formed.
171
+
172
+ [*Example 3*:
173
+
174
+ \`"decls.h"\`
175
+
176
+ ``` cpp
177
+ int f(); // #1, attached to the global module
178
+ int g(); // #2, attached to the global module
179
+ ```
180
+
181
+ Module interface of \`M\`
182
+
183
+ ``` cpp
184
+ module;
185
+ #include "decls.h"
186
+ export module M;
187
+ export using ::f; // OK: does not declare an entity, exports #1
188
+ int g(); // error: matches #2, but attached to M
189
+ export int h(); // #3
190
+ export int k(); // #4
191
+ ```
192
+
193
+ Other translation unit
194
+
195
+ ``` cpp
196
+ import M;
197
+ static int h(); // error: matches #3
198
+ int k(); // error: matches #4
199
+ ```
200
+
201
+ — *end example*]
202
+
203
+ As a consequence of these rules, all declarations of an entity are
204
+ attached to the same module; the entity is said to be *attached* to that
205
+ module.
206
+
207
+ After all adjustments of types (during which typedefs [[dcl.typedef]]
208
  are replaced by their definitions), the types specified by all
209
  declarations referring to a given variable or function shall be
210
  identical, except that declarations for an array object can specify
211
  array types that differ by the presence or absence of a major array
212
+ bound [[dcl.array]]. A violation of this rule on type identity does not
213
+ require a diagnostic.
214
 
215
+ [*Note 3*: Linkage to non-C++ declarations can be achieved using a
216
+ *linkage-specification* [[dcl.link]]. — *end note*]
217
+
218
+ A declaration D *names* an entity E if
219
+
220
+ - D contains a *lambda-expression* whose closure type is E,
221
+ - E is not a function or function template and D contains an
222
+ *id-expression*, *type-specifier*, *nested-name-specifier*,
223
+ *template-name*, or *concept-name* denoting E, or
224
+ - E is a function or function template and D contains an expression that
225
+ names E [[basic.def.odr]] or an *id-expression* that refers to a set
226
+ of overloads that contains E. \[*Note 4*: Non-dependent names in an
227
+ instantiated declaration do not refer to a set of overloads
228
+ [[temp.nondep]]. — *end note*]
229
+
230
+ A declaration is an *exposure* if it either names a TU-local entity
231
+ (defined below), ignoring
232
+
233
+ - the *function-body* for a non-inline function or function template
234
+ (but not the deduced return type for a (possibly instantiated)
235
+ definition of a function with a declared return type that uses a
236
+ placeholder type [[dcl.spec.auto]]),
237
+ - the *initializer* for a variable or variable template (but not the
238
+ variable’s type),
239
+ - friend declarations in a class definition, and
240
+ - any reference to a non-volatile const object or reference with
241
+ internal or no linkage initialized with a constant expression that is
242
+ not an odr-use [[basic.def.odr]],
243
+
244
+ or defines a constexpr variable initialized to a TU-local value (defined
245
+ below).
246
+
247
+ [*Note 5*: An inline function template can be an exposure even though
248
+ explicit specializations of it might be usable in other translation
249
+ units. — *end note*]
250
+
251
+ An entity is *TU-local* if it is
252
+
253
+ - a type, function, variable, or template that
254
+ - has a name with internal linkage, or
255
+ - does not have a name with linkage and is declared, or introduced by
256
+ a *lambda-expression*, within the definition of a TU-local entity,
257
+ - a type with no name that is defined outside a *class-specifier*,
258
+ function body, or *initializer* or is introduced by a
259
+ *defining-type-specifier* that is used to declare only TU-local
260
+ entities,
261
+ - a specialization of a TU-local template,
262
+ - a specialization of a template with any TU-local template argument, or
263
+ - a specialization of a template whose (possibly instantiated)
264
+ declaration is an exposure. \[*Note 6*: The specialization might have
265
+ been implicitly or explicitly instantiated. — *end note*]
266
+
267
+ A value or object is *TU-local* if either
268
+
269
+ - it is, or is a pointer to, a TU-local function or the object
270
+ associated with a TU-local variable,
271
+ - it is an object of class or array type and any of its subobjects or
272
+ any of the objects or functions to which its non-static data members
273
+ of reference type refer is TU-local and is usable in constant
274
+ expressions.
275
+
276
+ If a (possibly instantiated) declaration of, or a deduction guide for, a
277
+ non-TU-local entity in a module interface unit (outside the
278
+ *private-module-fragment*, if any) or module partition [[module.unit]]
279
+ is an exposure, the program is ill-formed. Such a declaration in any
280
+ other context is deprecated [[depr.local]].
281
+
282
+ If a declaration that appears in one translation unit names a TU-local
283
+ entity declared in another translation unit that is not a header unit,
284
+ the program is ill-formed. A declaration instantiated for a template
285
+ specialization [[temp.spec]] appears at the point of instantiation of
286
+ the specialization [[temp.point]].
287
+
288
+ [*Example 4*:
289
+
290
+ Translation unit #1
291
+
292
+ ``` cpp
293
+ export module A;
294
+ static void f() {}
295
+ inline void it() { f(); } // error: is an exposure of f
296
+ static inline void its() { f(); } // OK
297
+ template<int> void g() { its(); } // OK
298
+ template void g<0>();
299
+
300
+ decltype(f) *fp; // error: f (though not its type) is TU-local
301
+ auto &fr = f; // OK
302
+ constexpr auto &fr2 = fr; // error: is an exposure of f
303
+ constexpr static auto fp2 = fr; // OK
304
+
305
+ struct S { void (&ref)(); } s{f}; // OK, value is TU-local
306
+ constexpr extern struct W { S &s; } wrap{s}; // OK, value is not TU-local
307
+
308
+ static auto x = []{f();}; // OK
309
+ auto x2 = x; // error: the closure type is TU-local
310
+ int y = ([]{f();}(),0); // error: the closure type is not TU-local
311
+ int y2 = (x,0); // OK
312
+
313
+ namespace N {
314
+ struct A {};
315
+ void adl(A);
316
+ static void adl(int);
317
+ }
318
+ void adl(double);
319
+
320
+ inline void h(auto x) { adl(x); } // OK, but a specialization might be an exposure
321
+ ```
322
+
323
+ Translation unit #2
324
+
325
+ ``` cpp
326
+ module A;
327
+ void other() {
328
+ g<0>(); // OK, specialization is explicitly instantiated
329
+ g<1>(); // error: instantiation uses TU-local its
330
+ h(N::A{}); // error: overload set contains TU-local N::adl(int)
331
+ h(0); // OK, calls adl(double)
332
+ adl(N::A{}); // OK; N::adl(int) not found, calls N::adl(N::A)
333
+ fr(); // OK, calls f
334
+ constexpr auto ptr = fr; // error: fr is not usable in constant expressions here
335
+ }
336
+ ```
337
+
338
+ — *end example*]
339