tmp/tmp_uhfi_x6/{from.md → to.md}
RENAMED
|
@@ -1,29 +1,29 @@
|
|
| 1 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
| 2 |
|
| 3 |
-
All
|
| 4 |
-
|
| 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
|
| 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,
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
Linkage [[basic.link]] between C++ and non-C++ code fragments can be
|
| 19 |
achieved using a *linkage-specification*:
|
| 20 |
|
| 21 |
``` bnf
|
| 22 |
linkage-specification:
|
| 23 |
extern string-literal '{' declaration-seqₒₚₜ '}'
|
| 24 |
-
extern string-literal declaration
|
| 25 |
```
|
| 26 |
|
| 27 |
The *string-literal* indicates the required language linkage. This
|
| 28 |
document specifies the semantics for the *string-literal*s `"C"` and
|
| 29 |
`"C++"`. Use of a *string-literal* other than `"C"` or `"C++"` is
|
|
@@ -31,143 +31,119 @@ 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 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
vintage. — *end note*]
|
| 40 |
|
| 41 |
-
Every implementation shall provide for linkage to
|
| 42 |
-
|
| 43 |
-
`"C++"`.
|
| 44 |
|
| 45 |
[*Example 1*:
|
| 46 |
|
| 47 |
``` cpp
|
| 48 |
-
complex sqrt(complex); // C++{} linkage by default
|
| 49 |
extern "C" {
|
| 50 |
-
double sqrt(double); // C linkage
|
| 51 |
}
|
| 52 |
```
|
| 53 |
|
| 54 |
— *end example*]
|
| 55 |
|
| 56 |
-
A *module-import-declaration*
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
conditionally-supported with *implementation-defined* semantics.
|
| 60 |
|
| 61 |
Linkage specifications nest. When linkage specifications nest, the
|
| 62 |
-
innermost one determines the language linkage.
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
[*Example 2*:
|
| 70 |
|
| 71 |
``` cpp
|
| 72 |
-
extern "C" //
|
| 73 |
void f1(void(*pf)(int)); // pf is a pointer to a C function
|
| 74 |
|
| 75 |
extern "C" typedef void FUNC();
|
| 76 |
-
FUNC f2; //
|
| 77 |
-
//
|
| 78 |
|
| 79 |
-
extern "C" FUNC f3; //
|
| 80 |
|
| 81 |
-
void (*pf2)(FUNC*); // the
|
| 82 |
-
//
|
| 83 |
// pointer to C function''
|
| 84 |
extern "C" {
|
| 85 |
-
static void f4(); // the name of the function f4 has internal linkage
|
| 86 |
-
//
|
| 87 |
}
|
| 88 |
|
| 89 |
extern "C" void f5() {
|
| 90 |
-
extern void f4(); // OK
|
| 91 |
// obtained from previous declaration.
|
| 92 |
}
|
| 93 |
|
| 94 |
-
extern void f4(); // OK
|
| 95 |
// obtained from previous declaration.
|
| 96 |
|
| 97 |
void f6() {
|
| 98 |
-
extern void f4(); // OK
|
| 99 |
// obtained from previous declaration.
|
| 100 |
}
|
| 101 |
```
|
| 102 |
|
| 103 |
— *end example*]
|
| 104 |
|
| 105 |
A C language linkage is ignored in determining the language linkage of
|
| 106 |
-
|
| 107 |
-
functions.
|
| 108 |
|
| 109 |
[*Example 3*:
|
| 110 |
|
| 111 |
``` cpp
|
| 112 |
extern "C" typedef void FUNC_c();
|
| 113 |
|
| 114 |
class C {
|
| 115 |
-
void mf1(FUNC_c*); // the
|
| 116 |
-
//
|
| 117 |
|
| 118 |
-
FUNC_c mf2; // the
|
| 119 |
-
// C++{} language linkage
|
| 120 |
|
| 121 |
-
static FUNC_c* q; // the
|
| 122 |
-
//
|
| 123 |
};
|
| 124 |
|
| 125 |
extern "C" {
|
| 126 |
class X {
|
| 127 |
-
void mf(); // the
|
| 128 |
-
|
| 129 |
-
void mf2(void(*)()); // the name of the function mf2 has C++{} language linkage;
|
| 130 |
// the parameter has type ``pointer to C function''
|
| 131 |
};
|
| 132 |
}
|
| 133 |
```
|
| 134 |
|
| 135 |
— *end example*]
|
| 136 |
|
| 137 |
-
If two declarations
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
appear in different translation units. Except for functions with C++
|
| 143 |
-
linkage, a function declaration without a linkage specification shall
|
| 144 |
-
not precede the first linkage specification for that function. A
|
| 145 |
-
function can be declared without a linkage specification after an
|
| 146 |
-
explicit linkage specification has been seen; the linkage explicitly
|
| 147 |
-
specified in the earlier declaration is not affected by such a function
|
| 148 |
-
declaration.
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
declarations for a variable with C language linkage with the same name
|
| 155 |
-
(ignoring the namespace names that qualify it) that appear in different
|
| 156 |
-
namespace scopes refer to the same variable. An entity with C language
|
| 157 |
-
linkage shall not be declared with the same name as a variable in global
|
| 158 |
-
scope, unless both declarations denote the same entity; no diagnostic is
|
| 159 |
-
required if the declarations appear in different translation units. A
|
| 160 |
-
variable with C language linkage shall not be declared with the same
|
| 161 |
-
name as a function with C language linkage (ignoring the namespace names
|
| 162 |
-
that qualify the respective names); no diagnostic is required if the
|
| 163 |
-
declarations appear in different translation units.
|
| 164 |
-
|
| 165 |
-
[*Note 4*: Only one definition for an entity with a given name with C
|
| 166 |
-
language linkage may appear in the program (see [[basic.def.odr]]);
|
| 167 |
-
this implies that such an entity must not be defined in more than one
|
| 168 |
-
namespace scope. — *end note*]
|
| 169 |
|
| 170 |
[*Example 4*:
|
| 171 |
|
| 172 |
``` cpp
|
| 173 |
int x;
|
|
@@ -207,11 +183,11 @@ extern "C" {
|
|
| 207 |
extern "C" static void g(); // error
|
| 208 |
```
|
| 209 |
|
| 210 |
— *end example*]
|
| 211 |
|
| 212 |
-
[*Note
|
| 213 |
when indirecting through a pointer to C function, the function to which
|
| 214 |
the resulting lvalue refers is considered a C function. — *end note*]
|
| 215 |
|
| 216 |
Linkage from C++ to objects defined in other languages and to objects
|
| 217 |
defined in C++ from other languages is *implementation-defined* and
|
|
|
|
| 1 |
## Linkage specifications <a id="dcl.link">[[dcl.link]]</a>
|
| 2 |
|
| 3 |
+
All functions and variables whose names have external linkage and all
|
| 4 |
+
function types 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 might 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, functions, and
|
| 14 |
+
variables 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
|
| 22 |
linkage-specification:
|
| 23 |
extern string-literal '{' declaration-seqₒₚₜ '}'
|
| 24 |
+
extern string-literal name-declaration
|
| 25 |
```
|
| 26 |
|
| 27 |
The *string-literal* indicates the required language linkage. This
|
| 28 |
document specifies the semantics for the *string-literal*s `"C"` and
|
| 29 |
`"C++"`. Use of a *string-literal* other than `"C"` or `"C++"` is
|
|
|
|
| 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 |
+
*Recommended practice:* The spelling of the *string-literal* should be
|
| 37 |
+
taken from the document defining that language. For example, `Ada` (not
|
| 38 |
+
`ADA`) and `Fortran` or `FORTRAN`, depending on the vintage.
|
|
|
|
| 39 |
|
| 40 |
+
Every implementation shall provide for linkage to the C programming
|
| 41 |
+
language, `"C"`, and C++, `"C++"`.
|
|
|
|
| 42 |
|
| 43 |
[*Example 1*:
|
| 44 |
|
| 45 |
``` cpp
|
| 46 |
+
complex sqrt(complex); // C++{} language linkage by default
|
| 47 |
extern "C" {
|
| 48 |
+
double sqrt(double); // C language linkage
|
| 49 |
}
|
| 50 |
```
|
| 51 |
|
| 52 |
— *end example*]
|
| 53 |
|
| 54 |
+
A *module-import-declaration* appearing in a linkage specification with
|
| 55 |
+
other than C++ language linkage is conditionally-supported with
|
| 56 |
+
*implementation-defined* semantics.
|
|
|
|
| 57 |
|
| 58 |
Linkage specifications nest. When linkage specifications nest, the
|
| 59 |
+
innermost one determines the language linkage.
|
| 60 |
+
|
| 61 |
+
[*Note 3*: A linkage specification does not establish a
|
| 62 |
+
scope. — *end note*]
|
| 63 |
+
|
| 64 |
+
A *linkage-specification* shall inhabit a namespace scope. In a
|
| 65 |
+
*linkage-specification*, the specified language linkage applies to the
|
| 66 |
+
function types of all function declarators and to all functions and
|
| 67 |
+
variables whose names have external linkage.
|
| 68 |
|
| 69 |
[*Example 2*:
|
| 70 |
|
| 71 |
``` cpp
|
| 72 |
+
extern "C" // f1 and its function type have C language linkage;
|
| 73 |
void f1(void(*pf)(int)); // pf is a pointer to a C function
|
| 74 |
|
| 75 |
extern "C" typedef void FUNC();
|
| 76 |
+
FUNC f2; // f2 has C++{} language linkage and
|
| 77 |
+
// its type has C language linkage
|
| 78 |
|
| 79 |
+
extern "C" FUNC f3; // f3 and its type have C language linkage
|
| 80 |
|
| 81 |
+
void (*pf2)(FUNC*); // the variable pf2 has C++{} language linkage; its type
|
| 82 |
+
// is ``pointer to C++{} function that takes one parameter of type
|
| 83 |
// pointer to C function''
|
| 84 |
extern "C" {
|
| 85 |
+
static void f4(); // the name of the function f4 has internal linkage,
|
| 86 |
+
// so f4 has no language linkage; its type has C language linkage
|
| 87 |
}
|
| 88 |
|
| 89 |
extern "C" void f5() {
|
| 90 |
+
extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage)
|
| 91 |
// obtained from previous declaration.
|
| 92 |
}
|
| 93 |
|
| 94 |
+
extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage)
|
| 95 |
// obtained from previous declaration.
|
| 96 |
|
| 97 |
void f6() {
|
| 98 |
+
extern void f4(); // OK, name linkage (internal) and function type linkage (C language linkage)
|
| 99 |
// obtained from previous declaration.
|
| 100 |
}
|
| 101 |
```
|
| 102 |
|
| 103 |
— *end example*]
|
| 104 |
|
| 105 |
A C language linkage is ignored in determining the language linkage of
|
| 106 |
+
class members, friend functions with a trailing *requires-clause*, and
|
| 107 |
+
the function type of non-static class member functions.
|
| 108 |
|
| 109 |
[*Example 3*:
|
| 110 |
|
| 111 |
``` cpp
|
| 112 |
extern "C" typedef void FUNC_c();
|
| 113 |
|
| 114 |
class C {
|
| 115 |
+
void mf1(FUNC_c*); // the function mf1 and its type have C++{} language linkage;
|
| 116 |
+
// the parameter has type ``pointer to C function''
|
| 117 |
|
| 118 |
+
FUNC_c mf2; // the function mf2 and its type have C++{} language linkage
|
|
|
|
| 119 |
|
| 120 |
+
static FUNC_c* q; // the data member q has C++{} language linkage;
|
| 121 |
+
// its type is ``pointer to C function''
|
| 122 |
};
|
| 123 |
|
| 124 |
extern "C" {
|
| 125 |
class X {
|
| 126 |
+
void mf(); // the function mf and its type have C++{} language linkage
|
| 127 |
+
void mf2(void(*)()); // the function mf2 has C++{} language linkage;
|
|
|
|
| 128 |
// the parameter has type ``pointer to C function''
|
| 129 |
};
|
| 130 |
}
|
| 131 |
```
|
| 132 |
|
| 133 |
— *end example*]
|
| 134 |
|
| 135 |
+
If two declarations of an entity give it different language linkages,
|
| 136 |
+
the program is ill-formed; no diagnostic is required if neither
|
| 137 |
+
declaration is reachable from the other. A redeclaration of an entity
|
| 138 |
+
without a linkage specification inherits the language linkage of the
|
| 139 |
+
entity and (if applicable) its type.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
Two declarations declare the same entity if they (re)introduce the same
|
| 142 |
+
name, one declares a function or variable with C language linkage, and
|
| 143 |
+
the other declares such an entity or declares a variable that belongs to
|
| 144 |
+
the global scope.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
[*Example 4*:
|
| 147 |
|
| 148 |
``` cpp
|
| 149 |
int x;
|
|
|
|
| 183 |
extern "C" static void g(); // error
|
| 184 |
```
|
| 185 |
|
| 186 |
— *end example*]
|
| 187 |
|
| 188 |
+
[*Note 4*: Because the language linkage is part of a function type,
|
| 189 |
when indirecting through a pointer to C function, the function to which
|
| 190 |
the resulting lvalue refers is considered a C function. — *end note*]
|
| 191 |
|
| 192 |
Linkage from C++ to objects defined in other languages and to objects
|
| 193 |
defined in C++ from other languages is *implementation-defined* and
|