[dcl.link] (original) (raw)
9 Declarations [dcl.dcl]
9.11 Linkage specifications [dcl.link]
All function types, function names with external linkage, and variable names with external linkage have a language linkage.
[Note 1:
Some of the properties associated with an entity with language linkage are specific to each implementation and are not described here.
For example, a particular language linkage might be associated with a particular form of representing names of objects and functions with external linkage, or with a particular calling convention, etc.
— _end note_]
The default language linkage of all function types, function names, and variable names is C++ language linkage.
Two function types with different language linkages are distinct types even if they are otherwise identical.
Linkage between C++ and non-C++ code fragments can be achieved using a linkage-specification:
The string-literal indicates the required language linkage.
This document specifies the semantics for thestring-literals "C" and "C++".
Use of astring-literal other than "C" or "C++" is conditionally-supported, with implementation-defined semantics.
[Note 2:
Therefore, a linkage-specification with a string-literal that is unknown to the implementation requires a diagnostic.
— _end note_]
[Note 3:
It is recommended that the spelling of the string-literal be taken from the document defining that language.
For example, Ada(not ADA) and Fortran or FORTRAN, depending on the vintage.
— _end note_]
Every implementation shall provide for linkage to functions written in the C programming language,"C", and linkage to C++ functions, "C++".
[Example 1: complex sqrt(complex); extern "C" { double sqrt(double); } — _end example_]
A module-import-declaration appearing in a linkage specification with other than C++ language linkage is conditionally-supported withimplementation-defined semantics.
Linkage specifications nest.
When linkage specifications nest, the innermost one determines the language linkage.
A linkage specification does not establish a scope.
In alinkage-specification, the specified language linkage applies to the function types of all function declarators, function names with external linkage, and variable names with external linkage declared within the linkage-specification.
[Example 2: extern "C" void f1(void(*pf)(int)); extern "C" typedef void FUNC(); FUNC f2; extern "C" FUNC f3; void (*pf2)(FUNC*); extern "C" { static void f4(); } extern "C" void f5() { extern void f4(); } extern void f4(); void f6() { extern void f4(); } — _end example_]
A C language linkage is ignored in determining the language linkage of the names of class members and the function type of class member functions.
[Example 3: extern "C" typedef void FUNC_c();class C { void mf1(FUNC_c*); FUNC_c mf2; static FUNC_c* q; };extern "C" { class X { void mf(); void mf2(void(*)()); };} — _end example_]
If two declarations declare functions with the same name andparameter-type-list to be members of the same namespace or declare objects with the same name to be members of the same namespace and the declarations give the names different language linkages, the program is ill-formed; no diagnostic is required if the declarations appear in different translation units.
Except for functions with C++ linkage, a function declaration without a linkage specification shall not precede the first linkage specification for that function.
A function can be declared without a linkage specification after an explicit linkage specification has been seen; the linkage explicitly specified in the earlier declaration is not affected by such a function declaration.
At most one function with a particular name can have C language linkage.
Two declarations for a function with C language linkage with the same function name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same function.
Two declarations for a variable with C language linkage with the same name (ignoring the namespace names that qualify it) that appear in different namespace scopes refer to the same variable.
An entity with C language linkage shall not be declared with the same name as a variable in global scope, unless both declarations denote the same entity; no diagnostic is required if the declarations appear in different translation units.
A variable with C language linkage shall not be declared with the same name as a function with C language linkage (ignoring the namespace names that qualify the respective names); no diagnostic is required if the declarations appear in different translation units.
[Note 4:
Only one definition for an entity with a given name with C language linkage can appear in the program (see [basic.def.odr]); this implies that such an entity must not be defined in more than one namespace scope.
— _end note_]
[Example 4: int x;namespace A { extern "C" int f();extern "C" int g() { return 1; } extern "C" int h();extern "C" int x(); } namespace B { extern "C" int f(); extern "C" int g() { return 1; } } int A::f() { return 98; } extern "C" int h() { return 97; } — _end example_]
A declaration directly contained in alinkage-specificationis treated as if it contains theexternspecifier for the purpose of determining the linkage of the declared name and whether it is a definition.
Such a declaration shall not specify a storage class.
[Example 5: extern "C" double f();static double f(); extern "C" int i; extern "C" { int i; } extern "C" static void g(); — _end example_]
[Note 5:
Because the language linkage is part of a function type, when indirecting through a pointer to C function, the function to which the resulting lvalue refers is considered a C function.
— _end note_]
Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent.
Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved.