[dcl.link] (original) (raw)

9 Declarations [dcl]

All functions and variables whose names have external linkage and all function types 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, functions, and variables 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:

This document specifies the semantics for theunevaluated-strings "C" and "C++".

Use of an unevaluated-stringother than "C" or "C++" is conditionally-supported, with implementation-defined semantics.

[Note 3:

Therefore, a linkage-specification with a language linkage that is unknown to the implementation requires a diagnostic.

— _end note_]

Recommended practice: The spelling of the language linkage should be taken from the document defining that language.

For example, Ada (not ADA) andFortran or FORTRAN, depending on the vintage.

Every implementation shall provide for linkage to the C programming language,"C", and C++, "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.

[Note 4:

A linkage specification does not establish a scope.

— _end note_]

A linkage-specification shall inhabit a namespace scope.

In a linkage-specification, the specified language linkage applies to the function types of all function declarators and to all functions and variables whose names have external linkage.

[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 class members, friend functions with a trailing requires-clause, and the function type of non-static 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 of an entity give it different language linkages, the program is ill-formed; no diagnostic is required if neither declaration is reachable from the other.

A redeclaration of an entity without a linkage specification inherits the language linkage of the entity and (if applicable) its type.

Two declarations declare the same entity if they (re)introduce the same name, one declares a function or variable with C language linkage, and the other declares such an entity or declares a variable that belongs to the global scope.

[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 ([dcl.stc]) for the purpose of determining the linkage of the declared name and whether it is a definition.

[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 entities defined in other languages and to entities 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.