From Jason Turner

[basic.link]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpkbh053_l/{from.md → to.md} +92 -100
tmp/tmpkbh053_l/{from.md → to.md} RENAMED
@@ -8,11 +8,11 @@ declarations.
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
@@ -24,30 +24,33 @@ introduced by a declaration in another scope:
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
@@ -66,112 +69,71 @@ has its linkage determined as follows:
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
  }
106
  }
107
  ```
108
 
109
- Without the declaration at line \#2, the declaration at line \#3 would
110
- link with the declaration at line \#1. Because the declaration with
111
- internal linkage is hidden, however, \#3 is given external linkage,
112
- making the program ill-formed.
113
-
114
- — *end example*]
115
-
116
- When a block scope declaration of an entity with linkage is not found to
117
- refer to some other declaration, then that entity is a member of the
118
- innermost enclosing namespace. However such a declaration does not
119
- introduce the member name in its namespace scope.
120
-
121
- [*Example 2*:
122
-
123
- ``` cpp
124
- namespace X {
125
- void p() {
126
- q(); // error: q not yet declared
127
- extern void q(); // q is a member of namespace X
128
- }
129
-
130
- void middle() {
131
- q(); // error: q not yet declared
132
- }
133
-
134
- void q() { ... } // definition of X::q
135
- }
136
-
137
- void q() { ... } // some other, unrelated q
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
@@ -182,11 +144,11 @@ 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
 
@@ -202,32 +164,62 @@ int k(); // error: matches #4
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
@@ -237,18 +229,18 @@ A declaration is an *exposure* if it either names a TU-local entity
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
@@ -259,17 +251,17 @@ An entity is *TU-local* if it is
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
 
@@ -315,11 +307,11 @@ namespace N {
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
 
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 can 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
 
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
+ The name of an entity that belongs to a namespace scope
30
+ [[basic.scope.namespace]] has internal 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 declared in the purview of a module interface unit (outside
36
+ the *private-module-fragment*, if any) or module partition, or
37
  - it is explicitly declared `extern`, or
38
+ - it is inline, or
39
  - it was previously declared and the prior declaration did not have
40
  internal linkage; or
41
  - a data member of an anonymous union.
42
 
43
  [*Note 1*: An instantiated variable template that has const-qualified
44
  type can have external or module linkage, even if not declared
45
  `extern`. — *end note*]
46
 
47
  An unnamed namespace or a namespace declared directly or indirectly
48
  within an unnamed namespace has internal linkage. All other namespaces
49
+ have external linkage. The name of an entity that belongs to a namespace
50
+ scope that has not been given internal linkage above and that is the
51
+ name of
52
 
53
  - a variable; or
54
  - a function; or
55
  - a named class [[class.pre]], or an unnamed class defined in a typedef
56
  declaration in which the class has the typedef name for linkage
 
69
  - otherwise, if the declaration of the name is attached to a named
70
  module [[module.unit]] and is not exported [[module.interface]], the
71
  name has module linkage;
72
  - otherwise, the name has external linkage.
73
 
74
+ In addition, a member function, a static data member, a named class or
75
+ enumeration that inhabits a class scope, or an unnamed class or
76
+ enumeration defined in a typedef declaration that inhabits a class scope
77
+ such that the class or enumeration has the typedef name for linkage
78
+ purposes [[dcl.typedef]], has the same linkage, if any, as the name of
79
+ the class of which it is a member.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  [*Example 1*:
82
 
83
  ``` cpp
84
  static void f();
85
  extern "C" void h();
86
  static int i = 0; // #1
87
+ void q() {
88
  extern void f(); // internal linkage
89
+ extern void g(); // ::g, external linkage
90
  extern void h(); // C language linkage
91
  int i; // #2: i has no linkage
92
  {
93
  extern void f(); // internal linkage
94
+ extern int i; // #3: internal linkage
95
  }
96
  }
97
  ```
98
 
99
+ Even though the declaration at line \#2 hides the declaration at line
100
+ \#1, the declaration at line \#3 still redeclares \#1 and receives
101
+ internal linkage.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  — *end example*]
104
 
105
  Names not covered by these rules have no linkage. Moreover, except as
106
  noted, a name declared at block scope [[basic.scope.block]] has no
107
  linkage.
108
 
109
+ Two declarations of entities declare the same entity if, considering
110
+ declarations of unnamed types to introduce their names for linkage
111
+ purposes, if any [[dcl.typedef]], [[dcl.enum]], they correspond
112
+ [[basic.scope.scope]], have the same target scope that is not a function
113
+ or template parameter scope, and either
114
 
115
+ - they appear in the same translation unit, or
116
+ - they both declare names with module linkage and are attached to the
117
+ same module, or
118
+ - they both declare names with external linkage.
 
 
 
 
119
 
120
+ [*Note 2*: There are other circumstances in which declarations declare
121
+ the same entity
122
+ [[dcl.link]], [[temp.type]], [[temp.spec.partial]]. *end note*]
123
 
124
+ If a declaration H that declares a name with internal linkage precedes a
125
+ declaration D in another translation unit U and would declare the same
126
+ entity as D if it appeared in U, the program is ill-formed.
 
 
127
 
128
+ [*Note 3*: Such an H can appear only in a header unit. *end note*]
 
129
 
130
+ If two declarations of an entity are attached to different modules, the
131
+ program is ill-formed; no diagnostic is required if neither is reachable
132
+ from the other.
133
+
134
+ [*Example 2*:
135
 
136
  \`"decls.h"\`
137
 
138
  ``` cpp
139
  int f(); // #1, attached to the global module
 
144
 
145
  ``` cpp
146
  module;
147
  #include "decls.h"
148
  export module M;
149
+ export using ::f; // OK, does not declare an entity, exports #1
150
  int g(); // error: matches #2, but attached to M
151
  export int h(); // #3
152
  export int k(); // #4
153
  ```
154
 
 
164
 
165
  As a consequence of these rules, all declarations of an entity are
166
  attached to the same module; the entity is said to be *attached* to that
167
  module.
168
 
169
+ For any two declarations of an entity E:
170
+
171
+ - If one declares E to be a variable or function, the other shall
172
+ declare E as one of the same type.
173
+ - If one declares E to be an enumerator, the other shall do so.
174
+ - If one declares E to be a namespace, the other shall do so.
175
+ - If one declares E to be a type, the other shall declare E to be a type
176
+ of the same kind [[dcl.type.elab]].
177
+ - If one declares E to be a class template, the other shall do so with
178
+ the same kind and an equivalent *template-head* [[temp.over.link]].
179
+ \[*Note 4*: The declarations can supply different default template
180
+ arguments. — *end note*]
181
+ - If one declares E to be a function template or a (partial
182
+ specialization of a) variable template, the other shall declare E to
183
+ be one with an equivalent *template-head* and type.
184
+ - If one declares E to be an alias template, the other shall declare E
185
+ to be one with an equivalent *template-head* and *defining-type-id*.
186
+ - If one declares E to be a concept, the other shall do so.
187
+
188
+ Types are compared after all adjustments of types (during which typedefs
189
+ [[dcl.typedef]] are replaced by their definitions); declarations for an
190
+ array object can specify array types that differ by the presence or
191
+ absence of a major array bound [[dcl.array]]. No diagnostic is required
192
+ if neither declaration is reachable from the other.
193
+
194
+ [*Example 3*:
195
+
196
+ ``` cpp
197
+ int f(int x, int x); // error: different entities for x
198
+ void g(); // #1
199
+ void g(int); // OK, different entity from #1
200
+ int g(); // error: same entity as #1 with different type
201
+ void h(); // #2
202
+ namespace h {} // error: same entity as #2, but not a function
203
+ ```
204
+
205
+ — *end example*]
206
+
207
+ [*Note 5*: Linkage to non-C++ declarations can be achieved using a
208
  *linkage-specification* [[dcl.link]]. — *end note*]
209
 
210
  A declaration D *names* an entity E if
211
 
212
  - D contains a *lambda-expression* whose closure type is E,
213
  - E is not a function or function template and D contains an
214
  *id-expression*, *type-specifier*, *nested-name-specifier*,
215
  *template-name*, or *concept-name* denoting E, or
216
  - E is a function or function template and D contains an expression that
217
  names E [[basic.def.odr]] or an *id-expression* that refers to a set
218
+ of overloads that contains E. \[*Note 6*: Non-dependent names in an
219
  instantiated declaration do not refer to a set of overloads
220
+ [[temp.res]]. — *end note*]
221
 
222
  A declaration is an *exposure* if it either names a TU-local entity
223
  (defined below), ignoring
224
 
225
  - the *function-body* for a non-inline function or function template
 
229
  - the *initializer* for a variable or variable template (but not the
230
  variable’s type),
231
  - friend declarations in a class definition, and
232
  - any reference to a non-volatile const object or reference with
233
  internal or no linkage initialized with a constant expression that is
234
+ not an odr-use [[term.odr.use]],
235
 
236
  or defines a constexpr variable initialized to a TU-local value (defined
237
  below).
238
 
239
+ [*Note 7*: An inline function template can be an exposure even though
240
+ certain explicit specializations of it would be usable in other
241
+ translation units. — *end note*]
242
 
243
  An entity is *TU-local* if it is
244
 
245
  - a type, function, variable, or template that
246
  - has a name with internal linkage, or
 
251
  *defining-type-specifier* that is used to declare only TU-local
252
  entities,
253
  - a specialization of a TU-local template,
254
  - a specialization of a template with any TU-local template argument, or
255
  - a specialization of a template whose (possibly instantiated)
256
+ declaration is an exposure. \[*Note 8*: A specialization can be
257
+ produced by implicit or explicit instantiation. — *end note*]
258
 
259
  A value or object is *TU-local* if either
260
 
261
  - it is, or is a pointer to, a TU-local function or the object
262
+ associated with a TU-local variable, or
263
  - it is an object of class or array type and any of its subobjects or
264
  any of the objects or functions to which its non-static data members
265
  of reference type refer is TU-local and is usable in constant
266
  expressions.
267
 
 
307
  void adl(A);
308
  static void adl(int);
309
  }
310
  void adl(double);
311
 
312
+ inline void h(auto x) { adl(x); } // OK, but certain specializations are exposures
313
  ```
314
 
315
  Translation unit #2
316
 
317
  ``` cpp