Стандарт C++ 11 (1119564), страница 54
Текст из файла (страница 54)
— end note ] The default language linkage of all function types,function names, and variable names is C++ language linkage. Two function types with different languagelinkages are distinct types even if they are otherwise identical.2Linkage (3.5) between C++ and non-C++ code fragments can be achieved using a linkage-specification:linkage-specification:extern string-literal { declaration-seqopt }extern string-literal declarationThe string-literal indicates the required language linkage.
This International Standard specifies the semanticsfor the string-literals "C" and "C++". Use of a string-literal other than "C" or "C++" is conditionallysupported, with implementation-defined semantics. [ Note: Therefore, a linkage-specification with a stringliteral that is unknown to the implementation requires a diagnostic. — end note ] [ Note: It is recommendedthat 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 ]3Every implementation shall provide for linkage to functions written in the C programming language, "C",and linkage to C++ functions, "C++".
[ Example:complex sqrt(complex);extern "C" {double sqrt(double);}// C++ linkage by default// C linkage— end example ]4Linkage specifications nest. When linkage specifications nest, the innermost one determines the languagelinkage. A linkage specification does not establish a scope. A linkage-specification shall occur only innamespace scope (3.3). In a linkage-specification, the specified language linkage applies to the functiontypes of all function declarators, function names with external linkage, and variable names with externallinkage declared within the linkage-specification.
[ Example:extern "C" void f1(void(*pf)(int));// the name f1 and its function type have C language// linkage; pf is a pointer to a C functionextern "C" typedef void FUNC();FUNC f2;// the name f2 has C++ language linkage and the// function’s type has C language linkageextern "C" FUNC f3;// the name of function f3 and the function’s type// have C language linkagevoid (*pf2)(FUNC*);// the name of the variable pf2 has C++ linkage and// the type of pf2 is pointer to C++ function that// takes one parameter of type pointer to C functionextern "C" {static void f4();// the name of the function f4 has// internal linkage (not C language// linkage) and the function’s type§ 7.5174© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)// has C language linkage.}extern "C" void f5() {extern void f4();////////OK: Name linkage (internal)and function type linkage (Clanguage linkage) gotten fromprevious declaration.////////OK: Name linkage (internal)and function type linkage (Clanguage linkage) gotten fromprevious declaration.////////OK: Name linkage (internal)and function type linkage (Clanguage linkage) gotten fromprevious declaration.}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 classmembers and the function type of class member functions.
[ Example:extern "C" typedef void FUNC_c();class C {void mf1(FUNC_c*);// the name of the function mf1 and the member// function’s type have C++ language linkage; the// parameter has type pointer to C functionFUNC_c mf2;// the name of the function mf2 and the member// function’s type have C++ language linkagestatic FUNC_c* q;// the name of the data member q has C++ language// linkage and the data member’s type is pointer to// C function};extern "C" {class X {void mf();void mf2(void(*)());//////////the name of the function mf and the memberfunction’s type have C++ language linkagethe name of the function mf2 has C++ languagelinkage; the parameter has type pointer toC function};}— end example ]5If two declarations declare functions with the same name and parameter-type-list (8.3.5) to be members ofthe same namespace or declare objects with the same name to be members of the same namespace and thedeclarations give the names different language linkages, the program is ill-formed; no diagnostic is requiredif the declarations appear in different translation units.
Except for functions with C++ linkage, a functiondeclaration without a linkage specification shall not precede the first linkage specification for that function.§ 7.5© ISO/IEC 2011 – All rights reserved175ISO/IEC 14882:2011(E)A function can be declared without a linkage specification after an explicit linkage specification has beenseen; the linkage explicitly specified in the earlier declaration is not affected by such a function declaration.6At most one function with a particular name can have C language linkage. Two declarations for a functionwith C language linkage with the same function name (ignoring the namespace names that qualify it) thatappear in different namespace scopes refer to the same function. Two declarations for a variable with Clanguage linkage with the same name (ignoring the namespace names that qualify it) that appear in differentnamespace scopes refer to the same variable.
An entity with C language linkage shall not be declared withthe same name as an entity in global scope, unless both declarations denote the same entity; no diagnosticis required if the declarations appear in different translation units. A variable with C language linkage shallnot be declared with the same name as a function with C language linkage (ignoring the namespace namesthat qualify the respective names); no diagnostic is required if the declarations appear in different translationunits. [ Note: Only one definition for an entity with a given name with C language linkage may appear inthe program (see 3.2); this implies that such an entity must not be defined in more than one namespacescope.
— end note ] [ Example:int x;namespace A {extern "C" intextern "C" intextern "C" intextern "C" int}f();g() { return 1; }h();x();namespace B {extern "C" int f();extern "C" int g() { return 1; }// ill-formed: same name as global-space object x// A::f and B::f refer to the same function// ill-formed, the function g// with C language linkage has two definitions}int A::f() { return 98; }extern "C" int h() { return 97; }//definition for the function f with C language linkage// definition for the function h with C language linkage// A::h and ::h refer to the same function— end example ]7A declaration directly contained in a linkage-specification is treated as if it contains the extern specifier (7.1.1) 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:extern "C" double f();static double f();extern "C" int i;extern "C" {int i;}extern "C" static void g();// error// declaration// definition// error— end example ]8[ Note: Because the language linkage is part of a function type, when a pointer to C function (for example)is dereferenced, the function to which it refers is considered a C function.
— end note ]9Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languagesis implementation-defined and language-dependent. Only where the object layout strategies of two language§ 7.5176© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)implementations are similar enough can such linkage be achieved.7.6Attributes7.6.11Attribute syntax and semantics[dcl.attr][dcl.attr.grammar]Attributes specify additional information for various source constructs such as types, variables, names,blocks, or translation units.attribute-specifier-seq:attribute-specifier-seqopt attribute-specifierattribute-specifier:[ [ attribute-list ] ]alignment-specifieralignment-specifier:alignas ( type-id ...opt )alignas ( alignment-expression ...opt )attribute-list:attributeoptattribute-list , attributeoptattribute ...attribute-list , attribute ...attribute:attribute-token attribute-argument-clauseoptattribute-token:identifierattribute-scoped-tokenattribute-scoped-token:attribute-namespace :: identifierattribute-namespace:identifierattribute-argument-clause:( balanced-token-seq )balanced-token-seq:balanced-tokenoptbalanced-token-seq balanced-tokenbalanced-token:( balanced-token-seq )[ balanced-token-seq ]{ balanced-token-seq }any token other than a parenthesis, a bracket, or a brace2[ Note: For each individual attribute, the form of the balanced-token-seq will be specified.
— end note ]3In an attribute-list, an ellipsis may appear only if that attribute’s specification permits it. An attributefollowed by an ellipsis is a pack expansion (14.5.3). An attribute-specifier that contains no attributes has noeffect. The order in which the attribute-tokens appear in an attribute-list is not significant. If a keyword (2.12)or an alternative token (2.6) that satisfies the syntactic requirements of an identifier (2.11) is containedin an attribute-token, it is considered an identifier. No name lookup (3.4) is performed on any of theidentifiers contained in an attribute-token. The attribute-token determines additional requirements on theattribute-argument-clause (if any).
The use of an attribute-scoped-token is conditionally-supported, with§ 7.6.1© ISO/IEC 2011 – All rights reserved177ISO/IEC 14882:2011(E)implementation-defined behavior. [ Note: Each implementation should choose a distinctive name for theattribute-namespace in an attribute-scoped-token. — end note ]4Each attribute-specifier-seq is said to appertain to some entity or statement, identified by the syntacticcontext where it appears (Clause 6, Clause 7, Clause 8). If an attribute-specifier-seq that appertains tosome entity or statement contains an attribute that is not allowed to apply to that entity or statement, theprogram is ill-formed. If an attribute-specifier-seq appertains to a friend declaration (11.3), that declarationshall be a definition. No attribute-specifier-seq shall appertain to an explicit instantiation (14.7.2).5For an attribute-token not specified in this International Standard, the behavior is implementation-defined.6Two consecutive left square bracket tokens shall appear only when introducing an attribute-specifier.