General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
|
|
Provides for linkage between program units written in different programming languages.
This can also be used to detach a declaration from its module. SeeModule ownership. | (since C++20) |
extern string-literal{ declaration-seq (optional)} | (1) | ||||||||
extern string-literaldeclaration | (2) | ||||||||
string-literal | - | anunevaluated string literal that names the required language linkage |
declaration-seq | - | a sequence of declarations, which may include nested linkage specifications |
declaration | - | a declaration |
Contents |
Every function type, every function name withexternal linkage, and every variable name withexternal linkage, has a property calledlanguage linkage. Language linkage encapsulates the set of requirements necessary to link with a program unit written in another programming language:calling convention,name mangling (name decoration) algorithm, etc.
Only two language linkages are guaranteed to be supported:
extern"C"{int open(constchar*path_name,int flags);// C function declaration} int main(){int fd= open("test.txt",0);// calls a C function from a C++ program} // This C++ function can be called from C codeextern"C"void handler(int){std::cout<<"Callback invoked\n";// It can use C++}
Since language linkage is part of every function type, pointers to functions maintain language linkage as well. Language linkage of function types (which represents calling convention) and language linkage of function names (which represents name mangling) are independent of each other:
extern"C"void f1(void(*pf)());// declares a function f1 with C linkage,// which returns void and takes a pointer to a C function// which returns void and takes no parameters extern"C"typedefvoid FUNC();// declares FUNC as a C function type that returns void// and takes no parameters FUNC f2;// the name f2 has C++ linkage, but its type is C functionextern"C" FUNC f3;// the name f3 has C linkage and its type is C function void()void(*pf2)(FUNC*);// the name pf2 has C++ linkage, and its type is// "pointer to a C++ function which returns void and takes one// argument of type 'pointer to the C function which returns void// and takes no parameters'" extern"C"{staticvoid f4();// the name of the function f4 has internal linkage (no language)// but the function's type has C language linkage}
Iftwo 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 its type (if exists).
extern"C"int f();extern"C++"int f();// Error: different language linkages extern"C"int g();int g();// OK, has C language linkage int h();// has C++ language linkage by defaultextern"C"int h();// Error: different language linkages
When class members, friend functions with a trailingrequires clause,(since C++20) or non-static member functions appear in a"C" language block, the linkage of their types remains"C++" (but parameter types, if any, remain"C"):
extern"C"{class X{void mf();// the function mf and its type have C++ language linkagevoid mf2(void(*)());// the function mf2 has C++ language linkage;// the parameter has type “pointer to C function”};} template<typename T>struct A{struct B;}; extern"C"{template<typename T>struct A<T>::B{friendvoid f(B*) requirestrue{}// C language linkage ignored};} namespace Q{extern"C"void f();// not ill-formed}
LetC
be a declaration that declares a function or variable with"C" language linkage. If another declarationD
declares an entity with the same name, and it satisfies any of the following conditions,C
andD
declare the same entity:
D
declares a variable that belongs to the global scope.C
declares a variable,D
also declares a variable.C
declares a function,D
also declares a function.Unlikeregular redeclarations,C
andD
can have differenttarget scopes:
extern"C"{int x;int f();int g(){return1;}} namespace A{int x;// Error: redefines “x”int f();// OK, redeclares “f”int g(){return1;}// Error: redefines “g”}
However, therestrictions of such declarations still apply, which means they should either both declare functions or both declare variables, and the declared entities must have the same type:
namespace A{extern"C"int x();extern"C"int y();} int x;// Error: redeclares “x” as a different kind of entity namespace B{void y();// Error: redeclares “y” with a different type}
Language specifications can only appear innamespace scope.
The braces of the language specification do not establish a scope.
When language specifications nest, the innermost specification is the one that is in effect.
A declaration directly contained in a language linkage specification is treated as if it contains theextern specifier for the purpose of determining thelinkage of the declared name and whether it is adefinition.
extern"C"int x;// a declaration and not a definition// The above line is equivalent to extern "C" { extern int x; } extern"C"{int x;}// a declaration and definition extern"C"double f();staticdouble f();// error: linkage conflict extern"C"staticvoid g();// error: linkage conflict
extern"C" makes it possible to include header files containing declarations of C library functions in a C++ program, but if the same header file is shared with a C program,extern"C" (which is not allowed in C) must be hidden with an appropriate#ifdef, typically__cplusplus:
#ifdef __cplusplusextern"C"int foo(int,int);// C++ compiler sees this#elseint foo(int,int);// C compiler sees this#endif
The only modern compiler that differentiates function types with"C" and"C++" language linkages is Oracle Studio, others do not permit overloads that are only different in language linkage, including the overload sets required by the C++ standard (std::qsort,std::bsearch,std::signal,std::atexit, andstd::at_quick_exit):GCC bug 2316,Clang bug 6277,CWG issue 1555.
extern"C"using c_predfun=int(constvoid*,constvoid*);extern"C++"using cpp_predfun=int(constvoid*,constvoid*); // ill-formed, but accepted by most compilersstatic_assert(std::is_same<c_predfun, cpp_predfun>::value,"C and C++ language linkages shall not differentiate function types."); // following declarations do not declare overloads in most compilers// because c_predfun and cpp_predfun are considered to be the same typevoid qsort(void* base,std::size_t nmemb,std::size_t size, c_predfun* compar);void qsort(void* base,std::size_t nmemb,std::size_t size, cpp_predfun* compar);
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 4 | C++98 | names with internal linkage can have language linkages | limited to names with external linkage |
CWG 341 | C++98 | a function with"C" language linkage can have the same name as a global variable | the program is ill-formed in this case (no diagnostic required if they appear in different translation units) |
CWG 564 | C++98 | the program was ill-formed if two declarations only differ in language linkage specifications (i.e. different string literals following 'extern') | the actual language linkages given by the declarations are compared instead |
CWG 2460 | C++20 | friend functions with a trailingrequires clause and"C" language linkage had conflict behaviors | "C" language linkage is ignored in this case |
CWG 2483 | C++98 | the linkage of the types of static member functions appear in"C" language blocks was"C++" | the linkage is"C" |