Advertisement
Guest User

Untitled

a guest
Aug 8th, 2012
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. 7.5 Linkage specifications [dcl.link]
  2. 1 All function types, function names with external linkage, and variable
  3. names with external linkage have a language linkage. [ Note: Some
  4. of the properties associated with an entity with language linkage are
  5. specific to each implementation and are not described here. For example,
  6. a particular language linkage may be associated with a particular form
  7. of representing names of objects and functions with external linkage,
  8. or with a particular calling convention, etc. --end note ] The default
  9. language linkage of all function types, 96) During name lookup in a
  10. class hierarchy, some ambiguities may be resolved by considering whether
  11. one member hides the other along some paths (10.2). There is no such
  12. disambiguation when considering the set of names found as a result of
  13. following using-directives. function names, and variable names is C++
  14. language linkage. Two function types with different language linkages
  15. are distinct types even if they are otherwise identical.
  16. 2 Linkage (3.5) between C++ and non-C++ code fragments can be achieved
  17. using a linkage-specification:
  18. linkage-specification:
  19. extern string-literal { declaration-seqopt}
  20. extern string-literal declaration
  21. The string-literal indicates the required language linkage. This
  22. International Standard specifies the semantics for the string-literals
  23. "C" and "C++". Use of a string-literal other than "C" or "C++" is
  24. conditionallysupported, with implementation-defined semantics. [ Note:
  25. Therefore, a linkage-specification with a stringliteral that is unknown
  26. to the implementation requires a diagnostic. --end note ] [ Note: It is
  27. recommended that the spelling of the string-literal be taken from the
  28. document defining that language. For example, Ada (not ADA) and Fortran
  29. or FORTRAN, depending on the vintage. --end note ]
  30. 3 Every implementation shall provide for linkage to functions written in
  31. the C programming language, "C", and linkage to C++ functions, "C++". [
  32. Example:
  33. complex sqrt(complex); // C++ linkage by default
  34. extern "C" {
  35. double sqrt(double); // C linkage
  36. }
  37. --end example ]
  38. 4 Linkage specifications nest. When linkage specifications nest, the
  39. innermost one determines the language linkage. A linkage specification
  40. does not establish a scope. A linkage-specification shall occur only in
  41. namespace scope (3.3). In a linkage-specification, the specified language
  42. linkage applies to the function types of all function declarators,
  43. function names with external linkage, and variable names with external
  44. linkage declared within the linkage-specification. [ Example:
  45. extern "C" void f1(void(*pf)(int));
  46. // the name f1 and its function type have C language linkage;
  47. // pf is a pointer to a C function
  48. extern "C" typedef void FUNC();
  49. FUNC f2;
  50. // the name f2 has C++ language linkage and the function's type
  51. // has C language linkage
  52. extern "C" FUNC f3;
  53. // the name of function f3 and the function's type have C
  54. // language linkage
  55. void (*pf2)(FUNC*);
  56. // the name of the variable pf2 has C++ linkage and the type of
  57. // pf2 is pointer to C++ function that takes one parameter of
  58. // type pointer to C function
  59. extern "C" {
  60. static void f4();
  61. // the name of the function f4 has internal linkage (not C language
  62. // linkage) and the function's type has C language linkage.
  63. }
  64. extern "C" void f5() {
  65. extern void f4();
  66. // OK: Name linkage (internal) and function type linkage (C
  67. // language linkage) gotten from previous declaration.
  68. }
  69. extern void f4();
  70. // OK: Name linkage (internal) and function type linkage (C
  71. // language linkage) gotten from previous declaration.
  72. void f6() {
  73. extern void f4();
  74. // OK: Name linkage (internal) and function type linkage (C
  75. // language linkage) gotten from previous declaration.
  76. }
  77. --end example ] A C language linkage is ignored for the names of class
  78. members and the member function type of class member functions. [
  79. Example:
  80. extern "C" typedef void FUNC_c();
  81. class C {
  82. void mf1(FUNC_c*);
  83. // the name of the function mf1 and the member function's type
  84. // have C++ language linkage; the parameter has type pointer to
  85. // C function
  86. FUNC_c mf2;
  87. // the name of the function mf2 and the member function's type
  88. // have C++ language linkage
  89. static FUNC_c* q;
  90. // the name of the data member q has C++ language linkage and
  91. // the data member's type is pointer to C function
  92. };
  93. extern "C" {
  94. class X {
  95. void mf();
  96. // the name of the function mf and the member function's type
  97. // have C++ language linkage
  98. void mf2(void(*)());
  99. // the name of the function mf2 has C++ language linkage;
  100. // the parameter has type pointer to C function
  101. };
  102. }
  103. --end example ]
  104. 5 If two declarations declare functions with the same name and
  105. parameter-type-list (8.3.5) to be members of the same namespace or declare
  106. objects with the same name to be members of the same namespace and the
  107. declarations give the names different language linkages, the program
  108. is ill-formed; no diagnostic is required if the declarations appear in
  109. different translation units. Except for functions with C++ linkage, a
  110. function declaration without a linkage specification shall not precede
  111. the first linkage specification for that function. A function can
  112. be declared without a linkage specification after an explicit linkage
  113. specification has been seen; the linkage explicitly specified in the
  114. earlier declaration is not affected by such a function declaration.
  115. 6 At most one function with a particular name can have C language
  116. linkage. Two declarations for a function with C language linkage with
  117. the same function name (ignoring the namespace names that qualify it)
  118. that appear in different namespace scopes refer to the same function. Two
  119. declarations for a variable with C language linkage with the same name
  120. (ignoring the namespace names that qualify it) that appear in different
  121. namespace scopes refer to the same variable. An entity with C language
  122. linkage shall not be declared with the same name as an entity in global
  123. scope, unless both declarations denote the same entity; no diagnostic is
  124. required if the declarations appear in different translation units. A
  125. variable with C language linkage shall not be declared with the same
  126. name as a function with C language linkage (ignoring the namespace names
  127. that qualify the respective names); no diagnostic is required if the
  128. declarations appear in different translation units. [ Note: Only one
  129. definition for an entity with a given name with C language linkage may
  130. appear in the program (see 3.2); this implies that such an entity must
  131. not be defined in more than one namespace
  132. scope. --end note ] [ Example:
  133. int x;
  134. namespace A {
  135. extern "C" int f();
  136. extern "C" int g() { return 1; }
  137. extern "C" int h();
  138. extern "C" int x();
  139. // ill-formed: same name as global-space object x
  140. }
  141. namespace B {
  142. extern "C" int f();
  143. // A::f and B::f refer to the same function
  144. extern "C" int g() { return 1; }
  145. // ill-formed, the function g with C language linkage has two
  146. // definitions
  147. }
  148. int A::f() { return 98; }
  149. // definition for the function f with C language linkage
  150. extern "C" int h() { return 97; }
  151. // definition for the function h with C language linkage
  152. // A::h and ::h refer to the same function
  153. --end example ]
  154. 7 A declaration directly contained in a linkage-specification is
  155. treated as if it contains the extern specifier (7.1.1) for the purpose
  156. of determining the linkage of the declared name and whether it is a
  157. definition. Such a declaration shall not specify a storage class. [
  158. Example:
  159. extern "C" double f();
  160. static double f(); // error
  161. extern "C" int i; // declaration
  162. extern "C" {
  163. int i; // definition
  164. }
  165. extern "C" static void g(); // error
  166. --end example ]
  167. 8 [ Note: Because the language linkage is part of a function type, when
  168. a pointer to C function (for example) is dereferenced, the function to
  169. which it refers is considered a C function. --end note ]
  170. 9 Linkage from C++ to objects defined in other languages and to objects
  171. defined in C++ from other languages is implementation-defined and
  172. language-dependent. Only where the object layout strategies of two
  173. language implementations are similar enough can such linkage be achieved.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement