tmp/tmpnrmzm17p/{from.md → to.md}
RENAMED
|
@@ -1,17 +1,21 @@
|
|
| 1 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
| 2 |
|
| 3 |
All function types, function names with external linkage, and variable
|
| 4 |
-
names with external linkage have a *language linkage*.
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
with a particular
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
Linkage ([[basic.link]]) between C++and non-C++code fragments can be
|
| 15 |
achieved using a *linkage-specification*:
|
| 16 |
|
| 17 |
``` bnf
|
|
@@ -22,103 +26,112 @@ linkage-specification:
|
|
| 22 |
|
| 23 |
The *string-literal* indicates the required language linkage. This
|
| 24 |
International Standard specifies the semantics for the *string-literal*s
|
| 25 |
`"C"` and `"C++"`. Use of a *string-literal* other than `"C"` or `"C++"`
|
| 26 |
is conditionally-supported, with *implementation-defined* semantics.
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
that
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
Every implementation shall provide for linkage to functions written in
|
| 34 |
the C programming language, `"C"`, and linkage to C++functions, `"C++"`.
|
| 35 |
|
|
|
|
|
|
|
| 36 |
``` cpp
|
| 37 |
complex sqrt(complex); // C++linkage by default
|
| 38 |
extern "C" {
|
| 39 |
double sqrt(double); // C linkage
|
| 40 |
}
|
| 41 |
```
|
| 42 |
|
|
|
|
|
|
|
| 43 |
Linkage specifications nest. When linkage specifications nest, the
|
| 44 |
innermost one determines the language linkage. A linkage specification
|
| 45 |
does not establish a scope. A *linkage-specification* shall occur only
|
| 46 |
in namespace scope ([[basic.scope]]). In a *linkage-specification*, the
|
| 47 |
specified language linkage applies to the function types of all function
|
| 48 |
declarators, function names with external linkage, and variable names
|
| 49 |
with external linkage declared within the *linkage-specification*.
|
| 50 |
|
|
|
|
|
|
|
| 51 |
``` cpp
|
| 52 |
-
extern "C"
|
| 53 |
-
|
| 54 |
-
|
| 55 |
extern "C" typedef void FUNC();
|
| 56 |
FUNC f2; // the name f2 has C++language linkage and the
|
| 57 |
// function's type has C language linkage
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
//
|
|
|
|
| 63 |
extern "C" {
|
| 64 |
-
static void f4(); // the name of the function f4 has
|
| 65 |
-
//
|
| 66 |
-
// linkage) and the function's type
|
| 67 |
-
// has C language linkage.
|
| 68 |
}
|
| 69 |
|
| 70 |
extern "C" void f5() {
|
| 71 |
-
extern void f4(); // OK: Name linkage (internal)
|
| 72 |
-
//
|
| 73 |
-
// language linkage) obtained from
|
| 74 |
-
// previous declaration.
|
| 75 |
}
|
| 76 |
|
| 77 |
-
extern void f4(); // OK: Name linkage (internal)
|
| 78 |
-
//
|
| 79 |
-
// language linkage) obtained from
|
| 80 |
-
// previous declaration.
|
| 81 |
|
| 82 |
void f6() {
|
| 83 |
-
extern void f4(); // OK: Name linkage (internal)
|
| 84 |
-
//
|
| 85 |
-
// language linkage) obtained from
|
| 86 |
-
// previous declaration.
|
| 87 |
}
|
| 88 |
```
|
| 89 |
|
|
|
|
|
|
|
| 90 |
A C language linkage is ignored in determining the language linkage of
|
| 91 |
the names of class members and the function type of class member
|
| 92 |
functions.
|
| 93 |
|
|
|
|
|
|
|
| 94 |
``` cpp
|
| 95 |
extern "C" typedef void FUNC_c();
|
|
|
|
| 96 |
class C {
|
| 97 |
-
|
| 98 |
-
//
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
//
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
// C function
|
| 105 |
};
|
| 106 |
|
| 107 |
extern "C" {
|
| 108 |
class X {
|
| 109 |
-
|
| 110 |
-
//
|
| 111 |
-
|
| 112 |
-
//
|
| 113 |
-
// C function
|
| 114 |
};
|
| 115 |
}
|
| 116 |
```
|
| 117 |
|
|
|
|
|
|
|
| 118 |
If two declarations declare functions with the same name and
|
| 119 |
-
|
| 120 |
or declare objects with the same name to be members of the same
|
| 121 |
namespace and the declarations give the names different language
|
| 122 |
linkages, the program is ill-formed; no diagnostic is required if the
|
| 123 |
declarations appear in different translation units. Except for functions
|
| 124 |
with C++linkage, a function declaration without a linkage specification
|
|
@@ -133,20 +146,24 @@ Two declarations for a function with C language linkage with the same
|
|
| 133 |
function name (ignoring the namespace names that qualify it) that appear
|
| 134 |
in different namespace scopes refer to the same function. Two
|
| 135 |
declarations for a variable with C language linkage with the same name
|
| 136 |
(ignoring the namespace names that qualify it) that appear in different
|
| 137 |
namespace scopes refer to the same variable. An entity with C language
|
| 138 |
-
linkage shall not be declared with the same name as
|
| 139 |
scope, unless both declarations denote the same entity; no diagnostic is
|
| 140 |
required if the declarations appear in different translation units. A
|
| 141 |
variable with C language linkage shall not be declared with the same
|
| 142 |
name as a function with C language linkage (ignoring the namespace names
|
| 143 |
that qualify the respective names); no diagnostic is required if the
|
| 144 |
-
declarations appear in different translation units.
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
``` cpp
|
| 150 |
int x;
|
| 151 |
namespace A {
|
| 152 |
extern "C" int f();
|
|
@@ -155,39 +172,44 @@ namespace A {
|
|
| 155 |
extern "C" int x(); // ill-formed: same name as global-space object x
|
| 156 |
}
|
| 157 |
|
| 158 |
namespace B {
|
| 159 |
extern "C" int f(); // A::f and B::f refer to the same function
|
| 160 |
-
extern "C" int g() { return 1; } // ill-formed, the function g
|
| 161 |
-
// with C language linkage has two definitions
|
| 162 |
}
|
| 163 |
|
| 164 |
int A::f() { return 98; } // definition for the function f with C language linkage
|
| 165 |
extern "C" int h() { return 97; } // definition for the function h with C language linkage
|
| 166 |
// A::h and ::h refer to the same function
|
| 167 |
```
|
| 168 |
|
|
|
|
|
|
|
| 169 |
A declaration directly contained in a *linkage-specification* is treated
|
| 170 |
as if it contains the `extern` specifier ([[dcl.stc]]) for the purpose
|
| 171 |
of determining the linkage of the declared name and whether it is a
|
| 172 |
definition. Such a declaration shall not specify a storage class.
|
| 173 |
|
|
|
|
|
|
|
| 174 |
``` cpp
|
| 175 |
extern "C" double f();
|
| 176 |
static double f(); // error
|
| 177 |
extern "C" int i; // declaration
|
| 178 |
extern "C" {
|
| 179 |
int i; // definition
|
| 180 |
}
|
| 181 |
extern "C" static void g(); // error
|
| 182 |
```
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
| 187 |
|
| 188 |
Linkage from C++to objects defined in other languages and to objects
|
| 189 |
-
defined in C++from other languages is implementation-defined and
|
| 190 |
language-dependent. Only where the object layout strategies of two
|
| 191 |
language implementations are similar enough can such linkage be
|
| 192 |
achieved.
|
| 193 |
|
|
|
|
| 1 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
| 2 |
|
| 3 |
All function types, function names with external linkage, and variable
|
| 4 |
+
names with external linkage have a *language linkage*.
|
| 5 |
+
|
| 6 |
+
[*Note 1*: Some of the properties associated with an entity with
|
| 7 |
+
language linkage are specific to each implementation and are not
|
| 8 |
+
described here. For example, a particular language linkage may be
|
| 9 |
+
associated with a particular form of representing names of objects and
|
| 10 |
+
functions with external linkage, or with a particular calling
|
| 11 |
+
convention, etc. — *end note*]
|
| 12 |
+
|
| 13 |
+
The default language linkage of all function types, function names, and
|
| 14 |
+
variable names is C++language linkage. Two function types with different
|
| 15 |
+
language linkages are distinct types even if they are otherwise
|
| 16 |
+
identical.
|
| 17 |
|
| 18 |
Linkage ([[basic.link]]) between C++and non-C++code fragments can be
|
| 19 |
achieved using a *linkage-specification*:
|
| 20 |
|
| 21 |
``` bnf
|
|
|
|
| 26 |
|
| 27 |
The *string-literal* indicates the required language linkage. This
|
| 28 |
International Standard specifies the semantics for the *string-literal*s
|
| 29 |
`"C"` and `"C++"`. Use of a *string-literal* other than `"C"` or `"C++"`
|
| 30 |
is conditionally-supported, with *implementation-defined* semantics.
|
| 31 |
+
|
| 32 |
+
[*Note 2*: Therefore, a linkage-specification with a *string-literal*
|
| 33 |
+
that is unknown to the implementation requires a
|
| 34 |
+
diagnostic. — *end note*]
|
| 35 |
+
|
| 36 |
+
[*Note 3*: It is recommended that the spelling of the *string-literal*
|
| 37 |
+
be taken from the document defining that language. For example, `Ada`
|
| 38 |
+
(not `ADA`) and `Fortran` or `FORTRAN`, depending on the
|
| 39 |
+
vintage. — *end note*]
|
| 40 |
|
| 41 |
Every implementation shall provide for linkage to functions written in
|
| 42 |
the C programming language, `"C"`, and linkage to C++functions, `"C++"`.
|
| 43 |
|
| 44 |
+
[*Example 1*:
|
| 45 |
+
|
| 46 |
``` cpp
|
| 47 |
complex sqrt(complex); // C++linkage by default
|
| 48 |
extern "C" {
|
| 49 |
double sqrt(double); // C linkage
|
| 50 |
}
|
| 51 |
```
|
| 52 |
|
| 53 |
+
— *end example*]
|
| 54 |
+
|
| 55 |
Linkage specifications nest. When linkage specifications nest, the
|
| 56 |
innermost one determines the language linkage. A linkage specification
|
| 57 |
does not establish a scope. A *linkage-specification* shall occur only
|
| 58 |
in namespace scope ([[basic.scope]]). In a *linkage-specification*, the
|
| 59 |
specified language linkage applies to the function types of all function
|
| 60 |
declarators, function names with external linkage, and variable names
|
| 61 |
with external linkage declared within the *linkage-specification*.
|
| 62 |
|
| 63 |
+
[*Example 2*:
|
| 64 |
+
|
| 65 |
``` cpp
|
| 66 |
+
extern "C" // the name f1 and its function type have C language linkage;
|
| 67 |
+
void f1(void(*pf)(int)); // pf is a pointer to a C function
|
| 68 |
+
|
| 69 |
extern "C" typedef void FUNC();
|
| 70 |
FUNC f2; // the name f2 has C++language linkage and the
|
| 71 |
// function's type has C language linkage
|
| 72 |
+
|
| 73 |
+
extern "C" FUNC f3; // the name of function f3 and the function's type have C language linkage
|
| 74 |
+
|
| 75 |
+
void (*pf2)(FUNC*); // the name of the variable pf2 has C++linkage and the type
|
| 76 |
+
// of pf2 is ``pointer to C++function that takes one parameter of type
|
| 77 |
+
// pointer to C function''
|
| 78 |
extern "C" {
|
| 79 |
+
static void f4(); // the name of the function f4 has internal linkage (not C language linkage)
|
| 80 |
+
// and the function's type has C language linkage.
|
|
|
|
|
|
|
| 81 |
}
|
| 82 |
|
| 83 |
extern "C" void f5() {
|
| 84 |
+
extern void f4(); // OK: Name linkage (internal) and function type linkage (C language linkage)
|
| 85 |
+
// obtained from previous declaration.
|
|
|
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
+
extern void f4(); // OK: Name linkage (internal) and function type linkage (C language linkage)
|
| 89 |
+
// obtained from previous declaration.
|
|
|
|
|
|
|
| 90 |
|
| 91 |
void f6() {
|
| 92 |
+
extern void f4(); // OK: Name linkage (internal) and function type linkage (C language linkage)
|
| 93 |
+
// obtained from previous declaration.
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
```
|
| 96 |
|
| 97 |
+
— *end example*]
|
| 98 |
+
|
| 99 |
A C language linkage is ignored in determining the language linkage of
|
| 100 |
the names of class members and the function type of class member
|
| 101 |
functions.
|
| 102 |
|
| 103 |
+
[*Example 3*:
|
| 104 |
+
|
| 105 |
``` cpp
|
| 106 |
extern "C" typedef void FUNC_c();
|
| 107 |
+
|
| 108 |
class C {
|
| 109 |
+
void mf1(FUNC_c*); // the name of the function mf1 and the member function's type have
|
| 110 |
+
// C++language linkage; the parameter has type ``pointer to C function''
|
| 111 |
+
|
| 112 |
+
FUNC_c mf2; // the name of the function mf2 and the member function's type have
|
| 113 |
+
// C++language linkage
|
| 114 |
+
|
| 115 |
+
static FUNC_c* q; // the name of the data member q has C++language linkage and
|
| 116 |
+
// the data member's type is ``pointer to C function''
|
| 117 |
};
|
| 118 |
|
| 119 |
extern "C" {
|
| 120 |
class X {
|
| 121 |
+
void mf(); // the name of the function mf and the member function's type have
|
| 122 |
+
// C++language linkage
|
| 123 |
+
void mf2(void(*)()); // the name of the function mf2 has C++language linkage;
|
| 124 |
+
// the parameter has type ``pointer to C function''
|
|
|
|
| 125 |
};
|
| 126 |
}
|
| 127 |
```
|
| 128 |
|
| 129 |
+
— *end example*]
|
| 130 |
+
|
| 131 |
If two declarations declare functions with the same name and
|
| 132 |
+
parameter-type-list ([[dcl.fct]]) to be members of the same namespace
|
| 133 |
or declare objects with the same name to be members of the same
|
| 134 |
namespace and the declarations give the names different language
|
| 135 |
linkages, the program is ill-formed; no diagnostic is required if the
|
| 136 |
declarations appear in different translation units. Except for functions
|
| 137 |
with C++linkage, a function declaration without a linkage specification
|
|
|
|
| 146 |
function name (ignoring the namespace names that qualify it) that appear
|
| 147 |
in different namespace scopes refer to the same function. Two
|
| 148 |
declarations for a variable with C language linkage with the same name
|
| 149 |
(ignoring the namespace names that qualify it) that appear in different
|
| 150 |
namespace scopes refer to the same variable. An entity with C language
|
| 151 |
+
linkage shall not be declared with the same name as a variable in global
|
| 152 |
scope, unless both declarations denote the same entity; no diagnostic is
|
| 153 |
required if the declarations appear in different translation units. A
|
| 154 |
variable with C language linkage shall not be declared with the same
|
| 155 |
name as a function with C language linkage (ignoring the namespace names
|
| 156 |
that qualify the respective names); no diagnostic is required if the
|
| 157 |
+
declarations appear in different translation units.
|
| 158 |
+
|
| 159 |
+
[*Note 4*: Only one definition for an entity with a given name with C
|
| 160 |
+
language linkage may appear in the program (see [[basic.def.odr]]);
|
| 161 |
+
this implies that such an entity must not be defined in more than one
|
| 162 |
+
namespace scope. — *end note*]
|
| 163 |
+
|
| 164 |
+
[*Example 4*:
|
| 165 |
|
| 166 |
``` cpp
|
| 167 |
int x;
|
| 168 |
namespace A {
|
| 169 |
extern "C" int f();
|
|
|
|
| 172 |
extern "C" int x(); // ill-formed: same name as global-space object x
|
| 173 |
}
|
| 174 |
|
| 175 |
namespace B {
|
| 176 |
extern "C" int f(); // A::f and B::f refer to the same function
|
| 177 |
+
extern "C" int g() { return 1; } // ill-formed, the function g with C language linkage has two definitions
|
|
|
|
| 178 |
}
|
| 179 |
|
| 180 |
int A::f() { return 98; } // definition for the function f with C language linkage
|
| 181 |
extern "C" int h() { return 97; } // definition for the function h with C language linkage
|
| 182 |
// A::h and ::h refer to the same function
|
| 183 |
```
|
| 184 |
|
| 185 |
+
— *end example*]
|
| 186 |
+
|
| 187 |
A declaration directly contained in a *linkage-specification* is treated
|
| 188 |
as if it contains the `extern` specifier ([[dcl.stc]]) for the purpose
|
| 189 |
of determining the linkage of the declared name and whether it is a
|
| 190 |
definition. Such a declaration shall not specify a storage class.
|
| 191 |
|
| 192 |
+
[*Example 5*:
|
| 193 |
+
|
| 194 |
``` cpp
|
| 195 |
extern "C" double f();
|
| 196 |
static double f(); // error
|
| 197 |
extern "C" int i; // declaration
|
| 198 |
extern "C" {
|
| 199 |
int i; // definition
|
| 200 |
}
|
| 201 |
extern "C" static void g(); // error
|
| 202 |
```
|
| 203 |
|
| 204 |
+
— *end example*]
|
| 205 |
+
|
| 206 |
+
[*Note 5*: Because the language linkage is part of a function type,
|
| 207 |
+
when indirecting through a pointer to C function, the function to which
|
| 208 |
+
the resulting lvalue refers is considered a C function. — *end note*]
|
| 209 |
|
| 210 |
Linkage from C++to objects defined in other languages and to objects
|
| 211 |
+
defined in C++from other languages is *implementation-defined* and
|
| 212 |
language-dependent. Only where the object layout strategies of two
|
| 213 |
language implementations are similar enough can such linkage be
|
| 214 |
achieved.
|
| 215 |
|