tmp/tmpmjhsv79z/{from.md → to.md}
RENAMED
|
@@ -1,32 +1,24 @@
|
|
| 1 |
## Program and linkage <a id="basic.link">[[basic.link]]</a>
|
| 2 |
|
| 3 |
-
A *program* consists of one or more translation units [[lex.
|
| 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
|
| 14 |
-
|
| 15 |
-
introduced by a declaration in another scope:
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 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 |
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
|
|
@@ -38,18 +30,18 @@ The name of an entity that belongs to a namespace scope
|
|
| 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
|
| 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
|
|
@@ -62,12 +54,21 @@ name of
|
|
| 62 |
purposes [[dcl.enum]]; or
|
| 63 |
- a template
|
| 64 |
|
| 65 |
has its linkage determined as follows:
|
| 66 |
|
| 67 |
-
- if the
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -108,26 +109,29 @@ 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,
|
|
|
|
| 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
|
| 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
|
| 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 |
|
|
@@ -174,11 +178,11 @@ For any two declarations of an entity E:
|
|
| 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
|
| 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
|
|
@@ -202,22 +206,27 @@ void h(); // #2
|
|
| 202 |
namespace h {} // error: same entity as #2, but not a function
|
| 203 |
```
|
| 204 |
|
| 205 |
— *end example*]
|
| 206 |
|
| 207 |
-
[*Note
|
| 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
|
| 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
|
|
@@ -234,38 +243,46 @@ A declaration is an *exposure* if it either names a TU-local entity
|
|
| 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
|
| 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
|
|
|
|
| 246 |
- has a name with internal linkage, or
|
| 247 |
- does not have a name with linkage and is declared, or introduced by
|
| 248 |
a *lambda-expression*, within the definition of a TU-local entity,
|
| 249 |
- a type with no name that is defined outside a *class-specifier*,
|
| 250 |
function body, or *initializer* or is introduced by a
|
| 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
|
| 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,
|
| 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 |
|
| 268 |
If a (possibly instantiated) declaration of, or a deduction guide for, a
|
| 269 |
non-TU-local entity in a module interface unit (outside the
|
| 270 |
*private-module-fragment*, if any) or module partition [[module.unit]]
|
| 271 |
is an exposure, the program is ill-formed. Such a declaration in any
|
|
@@ -308,10 +325,20 @@ namespace N {
|
|
| 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
|
|
|
|
| 1 |
## Program and linkage <a id="basic.link">[[basic.link]]</a>
|
| 2 |
|
| 3 |
+
A *program* consists of one or more translation units [[lex.phases]]
|
| 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 has *external linkage*, *module linkage*, *internal linkage*, or
|
| 14 |
+
*no linkage*, as determined by the rules below.
|
|
|
|
| 15 |
|
| 16 |
+
[*Note 1*: All declarations of an entity with a name with internal
|
| 17 |
+
linkage appear in the same translation unit. All declarations of an
|
| 18 |
+
entity with module linkage are attached to the same
|
| 19 |
+
module. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
The name of an entity that belongs to a namespace scope
|
| 22 |
[[basic.scope.namespace]] has internal linkage if it is the name of
|
| 23 |
|
| 24 |
- a variable, variable template, function, or function template that is
|
|
|
|
| 30 |
- it is inline, or
|
| 31 |
- it was previously declared and the prior declaration did not have
|
| 32 |
internal linkage; or
|
| 33 |
- a data member of an anonymous union.
|
| 34 |
|
| 35 |
+
[*Note 2*: An instantiated variable template that has const-qualified
|
| 36 |
type can have external or module linkage, even if not declared
|
| 37 |
`extern`. — *end note*]
|
| 38 |
|
| 39 |
An unnamed namespace or a namespace declared directly or indirectly
|
| 40 |
within an unnamed namespace has internal linkage. All other namespaces
|
| 41 |
have external linkage. The name of an entity that belongs to a namespace
|
| 42 |
+
scope, that has not been given internal linkage above, and that is the
|
| 43 |
name of
|
| 44 |
|
| 45 |
- a variable; or
|
| 46 |
- a function; or
|
| 47 |
- a named class [[class.pre]], or an unnamed class defined in a typedef
|
|
|
|
| 54 |
purposes [[dcl.enum]]; or
|
| 55 |
- a template
|
| 56 |
|
| 57 |
has its linkage determined as follows:
|
| 58 |
|
| 59 |
+
- if the entity is a function or function template first declared in a
|
| 60 |
+
friend declaration and that declaration is a definition and the
|
| 61 |
+
enclosing class is defined within an *export-declaration*, the name
|
| 62 |
+
has the same linkage, if any, as the name of the enclosing class
|
| 63 |
+
[[class.friend]];
|
| 64 |
+
- otherwise, if the entity is a function or function template declared
|
| 65 |
+
in a friend declaration and a corresponding non-friend declaration is
|
| 66 |
+
reachable, the name has the linkage determined from that prior
|
| 67 |
+
declaration,
|
| 68 |
+
- otherwise, if the enclosing namespace has internal linkage, the name
|
| 69 |
+
has internal linkage;
|
| 70 |
- otherwise, if the declaration of the name is attached to a named
|
| 71 |
module [[module.unit]] and is not exported [[module.interface]], the
|
| 72 |
name has module linkage;
|
| 73 |
- otherwise, the name has external linkage.
|
| 74 |
|
|
|
|
| 109 |
|
| 110 |
Two declarations of entities declare the same entity if, considering
|
| 111 |
declarations of unnamed types to introduce their names for linkage
|
| 112 |
purposes, if any [[dcl.typedef]], [[dcl.enum]], they correspond
|
| 113 |
[[basic.scope.scope]], have the same target scope that is not a function
|
| 114 |
+
or template parameter scope, neither is a name-independent declaration,
|
| 115 |
+
and either
|
| 116 |
|
| 117 |
- they appear in the same translation unit, or
|
| 118 |
+
- they both declare type aliases or namespace aliases that have the same
|
| 119 |
+
underlying entity, or
|
| 120 |
- they both declare names with module linkage and are attached to the
|
| 121 |
same module, or
|
| 122 |
- they both declare names with external linkage.
|
| 123 |
|
| 124 |
+
[*Note 3*: There are other circumstances in which declarations declare
|
| 125 |
the same entity
|
| 126 |
[[dcl.link]], [[temp.type]], [[temp.spec.partial]]. — *end note*]
|
| 127 |
|
| 128 |
If a declaration H that declares a name with internal linkage precedes a
|
| 129 |
declaration D in another translation unit U and would declare the same
|
| 130 |
entity as D if it appeared in U, the program is ill-formed.
|
| 131 |
|
| 132 |
+
[*Note 4*: Such an H can appear only in a header unit. — *end note*]
|
| 133 |
|
| 134 |
If two declarations of an entity are attached to different modules, the
|
| 135 |
program is ill-formed; no diagnostic is required if neither is reachable
|
| 136 |
from the other.
|
| 137 |
|
|
|
|
| 178 |
- If one declares E to be a namespace, the other shall do so.
|
| 179 |
- If one declares E to be a type, the other shall declare E to be a type
|
| 180 |
of the same kind [[dcl.type.elab]].
|
| 181 |
- If one declares E to be a class template, the other shall do so with
|
| 182 |
the same kind and an equivalent *template-head* [[temp.over.link]].
|
| 183 |
+
\[*Note 5*: The declarations can supply different default template
|
| 184 |
arguments. — *end note*]
|
| 185 |
- If one declares E to be a function template or a (partial
|
| 186 |
specialization of a) variable template, the other shall declare E to
|
| 187 |
be one with an equivalent *template-head* and type.
|
| 188 |
- If one declares E to be an alias template, the other shall declare E
|
|
|
|
| 206 |
namespace h {} // error: same entity as #2, but not a function
|
| 207 |
```
|
| 208 |
|
| 209 |
— *end example*]
|
| 210 |
|
| 211 |
+
[*Note 6*: Linkage to non-C++ declarations can be achieved using a
|
| 212 |
*linkage-specification* [[dcl.link]]. — *end note*]
|
| 213 |
|
| 214 |
A declaration D *names* an entity E if
|
| 215 |
|
| 216 |
- D contains a *lambda-expression* whose closure type is E,
|
| 217 |
+
- D contains a *reflect-expression* or a *splice-specifier* that,
|
| 218 |
+
respectively, represents or designates E,
|
| 219 |
+
- D is an injected declaration [[expr.const]] whose characteristic
|
| 220 |
+
sequence contains a reflection that represents a data member
|
| 221 |
+
description (T, N, A, W, NUA) [[class.mem.general]] for which T is E,
|
| 222 |
- E is not a function or function template and D contains an
|
| 223 |
*id-expression*, *type-specifier*, *nested-name-specifier*,
|
| 224 |
*template-name*, or *concept-name* denoting E, or
|
| 225 |
- E is a function or function template and D contains an expression that
|
| 226 |
names E [[basic.def.odr]] or an *id-expression* that refers to a set
|
| 227 |
+
of overloads that contains E. \[*Note 7*: Non-dependent names in an
|
| 228 |
instantiated declaration do not refer to a set of overloads
|
| 229 |
[[temp.res]]. — *end note*]
|
| 230 |
|
| 231 |
A declaration is an *exposure* if it either names a TU-local entity
|
| 232 |
(defined below), ignoring
|
|
|
|
| 243 |
not an odr-use [[term.odr.use]],
|
| 244 |
|
| 245 |
or defines a constexpr variable initialized to a TU-local value (defined
|
| 246 |
below).
|
| 247 |
|
| 248 |
+
[*Note 8*: An inline function template can be an exposure even though
|
| 249 |
certain explicit specializations of it would be usable in other
|
| 250 |
translation units. — *end note*]
|
| 251 |
|
| 252 |
An entity is *TU-local* if it is
|
| 253 |
|
| 254 |
+
- a type, type alias, namespace, namespace alias, function, variable, or
|
| 255 |
+
template that
|
| 256 |
- has a name with internal linkage, or
|
| 257 |
- does not have a name with linkage and is declared, or introduced by
|
| 258 |
a *lambda-expression*, within the definition of a TU-local entity,
|
| 259 |
- a type with no name that is defined outside a *class-specifier*,
|
| 260 |
function body, or *initializer* or is introduced by a
|
| 261 |
*defining-type-specifier* that is used to declare only TU-local
|
| 262 |
entities,
|
| 263 |
- a specialization of a TU-local template,
|
| 264 |
- a specialization of a template with any TU-local template argument, or
|
| 265 |
- a specialization of a template whose (possibly instantiated)
|
| 266 |
+
declaration is an exposure. \[*Note 9*: A specialization can be
|
| 267 |
produced by implicit or explicit instantiation. — *end note*]
|
| 268 |
|
| 269 |
A value or object is *TU-local* if either
|
| 270 |
|
| 271 |
+
- it is of TU-local type,
|
| 272 |
- it is, or is a pointer to, a TU-local function or the object
|
| 273 |
+
associated with a TU-local variable,
|
| 274 |
- it is an object of class or array type and any of its subobjects or
|
| 275 |
any of the objects or functions to which its non-static data members
|
| 276 |
of reference type refer is TU-local and is usable in constant
|
| 277 |
+
expressions, or
|
| 278 |
+
- it is a reflection value [[basic.fundamental]] that represents
|
| 279 |
+
- an entity, value, or object that is TU-local,
|
| 280 |
+
- a direct base class relationship (D, B) [[class.derived.general]]
|
| 281 |
+
for which either D or B is TU-local, or
|
| 282 |
+
- a data member description (T, N, A, W, NUA) [[class.mem.general]]
|
| 283 |
+
for which T is TU-local.
|
| 284 |
|
| 285 |
If a (possibly instantiated) declaration of, or a deduction guide for, a
|
| 286 |
non-TU-local entity in a module interface unit (outside the
|
| 287 |
*private-module-fragment*, if any) or module partition [[module.unit]]
|
| 288 |
is an exposure, the program is ill-formed. Such a declaration in any
|
|
|
|
| 325 |
static void adl(int);
|
| 326 |
}
|
| 327 |
void adl(double);
|
| 328 |
|
| 329 |
inline void h(auto x) { adl(x); } // OK, but certain specializations are exposures
|
| 330 |
+
|
| 331 |
+
constexpr std::meta::info r1 = ^^g<0>; // OK
|
| 332 |
+
namespace N2 {
|
| 333 |
+
static constexpr std::meta::info r2 = ^^g<1>; // OK, r2 is TU-local
|
| 334 |
+
}
|
| 335 |
+
constexpr std::meta::info r3 = ^^f; // error: r3 is an exposure of f
|
| 336 |
+
|
| 337 |
+
constexpr auto ctx = std::meta::access_context::current();
|
| 338 |
+
constexpr std::meta::info r4 =
|
| 339 |
+
std::meta::members_of(^^N2, ctx)[0]; // error: r4 is an exposure of N2::r2
|
| 340 |
```
|
| 341 |
|
| 342 |
Translation unit #2
|
| 343 |
|
| 344 |
``` cpp
|