tmp/tmp91_eqb2t/{from.md → to.md}
RENAMED
|
@@ -9,27 +9,27 @@ 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
|
| 15 |
-
language linkages are distinct types even if they are
|
| 16 |
-
identical.
|
| 17 |
|
| 18 |
-
Linkage
|
| 19 |
achieved using a *linkage-specification*:
|
| 20 |
|
| 21 |
``` bnf
|
| 22 |
linkage-specification:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
```
|
| 26 |
|
| 27 |
The *string-literal* indicates the required language linkage. This
|
| 28 |
-
|
| 29 |
-
`"C
|
| 30 |
-
|
| 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 |
|
|
@@ -37,27 +37,33 @@ diagnostic. — *end note*]
|
|
| 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,
|
|
|
|
| 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
|
| 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*:
|
|
@@ -65,17 +71,17 @@ with external linkage declared within the *linkage-specification*.
|
|
| 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 |
}
|
|
@@ -105,39 +111,39 @@ functions.
|
|
| 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
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
function can be declared without a linkage specification after an
|
| 140 |
explicit linkage specification has been seen; the linkage explicitly
|
| 141 |
specified in the earlier declaration is not affected by such a function
|
| 142 |
declaration.
|
| 143 |
|
|
@@ -167,28 +173,28 @@ namespace scope. — *end note*]
|
|
| 167 |
int x;
|
| 168 |
namespace A {
|
| 169 |
extern "C" int f();
|
| 170 |
extern "C" int g() { return 1; }
|
| 171 |
extern "C" int h();
|
| 172 |
-
extern "C" int 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; } //
|
| 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
|
| 189 |
-
|
| 190 |
definition. Such a declaration shall not specify a storage class.
|
| 191 |
|
| 192 |
[*Example 5*:
|
| 193 |
|
| 194 |
``` cpp
|
|
|
|
| 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
|
| 15 |
+
different language linkages are distinct types even if they are
|
| 16 |
+
otherwise 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 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
|
| 30 |
+
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 |
|
|
|
|
| 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,
|
| 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* shall not be directly contained in a
|
| 57 |
+
*linkage-specification*. A *module-import-declaration* appearing in a
|
| 58 |
+
linkage specification with other than C++ language linkage is
|
| 59 |
+
conditionally-supported with *implementation-defined* semantics.
|
| 60 |
+
|
| 61 |
Linkage specifications nest. When linkage specifications nest, the
|
| 62 |
innermost one determines the language linkage. A linkage specification
|
| 63 |
does not establish a scope. A *linkage-specification* shall occur only
|
| 64 |
+
in namespace scope [[basic.scope]]. In a *linkage-specification*, the
|
| 65 |
specified language linkage applies to the function types of all function
|
| 66 |
declarators, function names with external linkage, and variable names
|
| 67 |
with external linkage declared within the *linkage-specification*.
|
| 68 |
|
| 69 |
[*Example 2*:
|
|
|
|
| 71 |
``` cpp
|
| 72 |
extern "C" // the name 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; // the name f2 has C++{} language linkage and the
|
| 77 |
// function's type has C language linkage
|
| 78 |
|
| 79 |
extern "C" FUNC f3; // the name of function f3 and the function's type have C language linkage
|
| 80 |
|
| 81 |
+
void (*pf2)(FUNC*); // the name of the variable pf2 has C++{} linkage and the type
|
| 82 |
+
// of pf2 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 (not C language linkage)
|
| 86 |
// and the function's type has C language linkage.
|
| 87 |
}
|
|
|
|
| 111 |
``` cpp
|
| 112 |
extern "C" typedef void FUNC_c();
|
| 113 |
|
| 114 |
class C {
|
| 115 |
void mf1(FUNC_c*); // the name of the function mf1 and the member function's type have
|
| 116 |
+
// C++{} language linkage; the parameter has type ``pointer to C function''
|
| 117 |
|
| 118 |
FUNC_c mf2; // the name of the function mf2 and the member function's type have
|
| 119 |
+
// C++{} language linkage
|
| 120 |
|
| 121 |
+
static FUNC_c* q; // the name of the data member q has C++{} language linkage and
|
| 122 |
// the data member's type is ``pointer to C function''
|
| 123 |
};
|
| 124 |
|
| 125 |
extern "C" {
|
| 126 |
class X {
|
| 127 |
void mf(); // the name of the function mf and the member function's type have
|
| 128 |
+
// C++{} language linkage
|
| 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 declare functions with the same name and
|
| 138 |
+
parameter-type-list [[dcl.fct]] to be members of the same namespace or
|
| 139 |
+
declare objects with the same name to be members of the same namespace
|
| 140 |
+
and the declarations give the names different language linkages, the
|
| 141 |
+
program is ill-formed; no diagnostic is required if the declarations
|
| 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 |
|
|
|
|
| 173 |
int x;
|
| 174 |
namespace A {
|
| 175 |
extern "C" int f();
|
| 176 |
extern "C" int g() { return 1; }
|
| 177 |
extern "C" int h();
|
| 178 |
+
extern "C" int x(); // error: same name as global-space object x
|
| 179 |
}
|
| 180 |
|
| 181 |
namespace B {
|
| 182 |
extern "C" int f(); // A::f and B::f refer to the same function
|
| 183 |
+
extern "C" int g() { return 1; } // error: the function g with C language linkage has two definitions
|
| 184 |
}
|
| 185 |
|
| 186 |
int A::f() { return 98; } // definition for the function f with C language linkage
|
| 187 |
extern "C" int h() { return 97; } // definition for the function h with C language linkage
|
| 188 |
// A::h and ::h refer to the same function
|
| 189 |
```
|
| 190 |
|
| 191 |
— *end example*]
|
| 192 |
|
| 193 |
A declaration directly contained in a *linkage-specification* is treated
|
| 194 |
+
as if it contains the `extern` specifier [[dcl.stc]] for the purpose of
|
| 195 |
+
determining the linkage of the declared name and whether it is a
|
| 196 |
definition. Such a declaration shall not specify a storage class.
|
| 197 |
|
| 198 |
[*Example 5*:
|
| 199 |
|
| 200 |
``` cpp
|