tmp/tmpc8sublzj/{from.md → to.md}
RENAMED
|
@@ -24,13 +24,13 @@ introduced by a declaration in another scope:
|
|
| 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-
|
| 30 |
-
|
| 31 |
-
|
| 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
|
|
@@ -43,54 +43,59 @@ namespace if it is the name of
|
|
| 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 |
-
- an enumerator belonging to an enumeration with linkage; or
|
| 49 |
- a template.
|
| 50 |
|
| 51 |
In addition, a member function, static data member, a named class or
|
| 52 |
enumeration of class scope, or an unnamed class or enumeration defined
|
| 53 |
in a class-scope typedef declaration such that the class or enumeration
|
| 54 |
-
has the typedef name for linkage purposes ([[dcl.typedef]]), has
|
| 55 |
-
|
| 56 |
|
| 57 |
The name of a function declared in block scope and the name of a
|
| 58 |
variable declared by a block scope `extern` declaration have linkage. If
|
| 59 |
there is a visible declaration of an entity with linkage having the same
|
| 60 |
name and type, ignoring entities declared outside the innermost
|
| 61 |
enclosing namespace scope, the block scope declaration declares that
|
| 62 |
same entity and receives the linkage of the previous declaration. If
|
| 63 |
there is more than one such matching entity, the program is ill-formed.
|
| 64 |
Otherwise, if no matching entity is found, the block scope entity
|
| 65 |
-
receives external linkage.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
``` cpp
|
| 68 |
static void f();
|
| 69 |
static int i = 0; // #1
|
| 70 |
void g() {
|
| 71 |
extern void f(); // internal linkage
|
| 72 |
-
int i; // #2 i has no linkage
|
| 73 |
{
|
| 74 |
extern void f(); // internal linkage
|
| 75 |
-
extern int i; // #3 external linkage
|
| 76 |
}
|
| 77 |
}
|
| 78 |
```
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
When a block scope declaration of an entity with linkage is not found to
|
| 88 |
refer to some other declaration, then that entity is a member of the
|
| 89 |
innermost enclosing namespace. However such a declaration does not
|
| 90 |
introduce the member name in its namespace scope.
|
| 91 |
|
|
|
|
|
|
|
| 92 |
``` cpp
|
| 93 |
namespace X {
|
| 94 |
void p() {
|
| 95 |
q(); // error: q not yet declared
|
| 96 |
extern void q(); // q is a member of namespace X
|
|
@@ -98,24 +103,26 @@ namespace X {
|
|
| 98 |
|
| 99 |
void middle() {
|
| 100 |
q(); // error: q not yet declared
|
| 101 |
}
|
| 102 |
|
| 103 |
-
void q() {
|
| 104 |
}
|
| 105 |
|
| 106 |
-
void q() {
|
| 107 |
```
|
| 108 |
|
|
|
|
|
|
|
| 109 |
Names not covered by these rules have no linkage. Moreover, except as
|
| 110 |
noted, a name declared at block scope ([[basic.scope.block]]) has no
|
| 111 |
linkage. A type is said to have linkage if and only if:
|
| 112 |
|
| 113 |
- it is a class or enumeration type that is named (or has a name for
|
| 114 |
linkage purposes ([[dcl.typedef]])) and the name has linkage; or
|
| 115 |
-
- it is an unnamed class or enumeration
|
| 116 |
-
or
|
| 117 |
- it is a specialization of a class template (Clause [[temp]])[^10]; or
|
| 118 |
- it is a fundamental type ([[basic.fundamental]]); or
|
| 119 |
- it is a compound type ([[basic.compound]]) other than a class or
|
| 120 |
enumeration, compounded exclusively from types that have linkage; or
|
| 121 |
- it is a cv-qualified ([[basic.type.qualifier]]) version of a type
|
|
@@ -128,18 +135,20 @@ function with external linkage unless
|
|
| 128 |
- the entity is declared within an unnamed namespace (
|
| 129 |
[[namespace.def]]), or
|
| 130 |
- the entity is not odr-used ([[basic.def.odr]]) or is defined in the
|
| 131 |
same translation unit.
|
| 132 |
|
| 133 |
-
In other words, a type without linkage contains a class or
|
| 134 |
-
that cannot be named outside its translation unit. An entity
|
| 135 |
-
external linkage declared using such a type could not correspond to
|
| 136 |
-
other entity in another translation unit of the program and thus
|
| 137 |
-
defined in the translation unit if it is odr-used. Also note
|
| 138 |
-
classes with linkage may contain members whose types do not have
|
| 139 |
linkage, and that typedef names are ignored in the determination of
|
| 140 |
-
whether a type has linkage.
|
|
|
|
|
|
|
| 141 |
|
| 142 |
``` cpp
|
| 143 |
template <class T> struct B {
|
| 144 |
void g(T) { }
|
| 145 |
void h(T);
|
|
@@ -154,13 +163,15 @@ void f() {
|
|
| 154 |
ba.h(a); // error: B<A>::h(A) not defined in the translation unit
|
| 155 |
i(ba, a); // OK
|
| 156 |
}
|
| 157 |
```
|
| 158 |
|
|
|
|
|
|
|
| 159 |
Two names that are the same (Clause [[basic]]) and that are declared in
|
| 160 |
different scopes shall denote the same variable, function, type,
|
| 161 |
-
|
| 162 |
|
| 163 |
- both names have external linkage or else both names have internal
|
| 164 |
linkage and are declared in the same translation unit; and
|
| 165 |
- both names refer to members of the same namespace or to members, not
|
| 166 |
by inheritance, of the same class; and
|
|
@@ -175,8 +186,8 @@ declarations referring to a given variable or function shall be
|
|
| 175 |
identical, except that declarations for an array object can specify
|
| 176 |
array types that differ by the presence or absence of a major array
|
| 177 |
bound ([[dcl.array]]). A violation of this rule on type identity does
|
| 178 |
not require a diagnostic.
|
| 179 |
|
| 180 |
-
Linkage to non-C++declarations can be achieved using a
|
| 181 |
-
*linkage-specification* ([[dcl.link]]).
|
| 182 |
|
|
|
|
| 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
|
|
|
|
| 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 |
}
|
| 80 |
}
|
| 81 |
```
|
| 82 |
|
| 83 |
+
Without the declaration at line \#2, the declaration at line \#3 would
|
| 84 |
+
link with the declaration at line \#1. Because the declaration with
|
| 85 |
+
internal linkage is hidden, however, \#3 is given external linkage,
|
| 86 |
+
making the program ill-formed.
|
| 87 |
+
|
| 88 |
+
— *end example*]
|
| 89 |
|
| 90 |
When a block scope declaration of an entity with linkage is not found to
|
| 91 |
refer to some other declaration, then that entity is a member of the
|
| 92 |
innermost enclosing namespace. However such a declaration does not
|
| 93 |
introduce the member name in its namespace scope.
|
| 94 |
|
| 95 |
+
[*Example 2*:
|
| 96 |
+
|
| 97 |
``` cpp
|
| 98 |
namespace X {
|
| 99 |
void p() {
|
| 100 |
q(); // error: q not yet declared
|
| 101 |
extern void q(); // q is a member of namespace X
|
|
|
|
| 103 |
|
| 104 |
void middle() {
|
| 105 |
q(); // error: q not yet declared
|
| 106 |
}
|
| 107 |
|
| 108 |
+
void q() { ... } // definition of X::q
|
| 109 |
}
|
| 110 |
|
| 111 |
+
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
|
|
|
|
| 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);
|
|
|
|
| 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
|
|
|
|
| 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 |
|