Name-lookup.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Name lookup</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Name lookup">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Name lookup">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings" rel="up" title="C++ Misunderstandings">
  30. <link href="Temporaries.html#Temporaries" rel="next" title="Temporaries">
  31. <link href="Static-Definitions.html#Static-Definitions" rel="prev" title="Static Definitions">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Name-lookup"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Temporaries.html#Temporaries" accesskey="n" rel="next">Temporaries</a>, Previous: <a href="Static-Definitions.html#Static-Definitions" accesskey="p" rel="prev">Static Definitions</a>, Up: <a href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings" accesskey="u" rel="up">C++ Misunderstandings</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Name-Lookup_002c-Templates_002c-and-Accessing-Members-of-Base-Classes"></a>
  69. <h4 class="subsection">13.7.2 Name Lookup, Templates, and Accessing Members of Base Classes</h4>
  70. <a name="index-base-class-members"></a>
  71. <a name="index-two_002dstage-name-lookup"></a>
  72. <a name="index-dependent-name-lookup"></a>
  73. <p>The C++ standard prescribes that all names that are not dependent on
  74. template parameters are bound to their present definitions when parsing
  75. a template function or class.<a name="DOCF5" href="#FOOT5"><sup>5</sup></a> Only names that are dependent are looked up at the point
  76. of instantiation. For example, consider
  77. </p>
  78. <div class="smallexample">
  79. <pre class="smallexample"> void foo(double);
  80. struct A {
  81. template &lt;typename T&gt;
  82. void f () {
  83. foo (1); // <span class="roman">1</span>
  84. int i = N; // <span class="roman">2</span>
  85. T t;
  86. t.bar(); // <span class="roman">3</span>
  87. foo (t); // <span class="roman">4</span>
  88. }
  89. static const int N;
  90. };
  91. </pre></div>
  92. <p>Here, the names <code>foo</code> and <code>N</code> appear in a context that does
  93. not depend on the type of <code>T</code>. The compiler will thus require that
  94. they are defined in the context of use in the template, not only before
  95. the point of instantiation, and will here use <code>::foo(double)</code> and
  96. <code>A::N</code>, respectively. In particular, it will convert the integer
  97. value to a <code>double</code> when passing it to <code>::foo(double)</code>.
  98. </p>
  99. <p>Conversely, <code>bar</code> and the call to <code>foo</code> in the fourth marked
  100. line are used in contexts that do depend on the type of <code>T</code>, so
  101. they are only looked up at the point of instantiation, and you can
  102. provide declarations for them after declaring the template, but before
  103. instantiating it. In particular, if you instantiate <code>A::f&lt;int&gt;</code>,
  104. the last line will call an overloaded <code>::foo(int)</code> if one was
  105. provided, even if after the declaration of <code>struct A</code>.
  106. </p>
  107. <p>This distinction between lookup of dependent and non-dependent names is
  108. called two-stage (or dependent) name lookup. G++ implements it
  109. since version 3.4.
  110. </p>
  111. <p>Two-stage name lookup sometimes leads to situations with behavior
  112. different from non-template codes. The most common is probably this:
  113. </p>
  114. <div class="smallexample">
  115. <pre class="smallexample"> template &lt;typename T&gt; struct Base {
  116. int i;
  117. };
  118. template &lt;typename T&gt; struct Derived : public Base&lt;T&gt; {
  119. int get_i() { return i; }
  120. };
  121. </pre></div>
  122. <p>In <code>get_i()</code>, <code>i</code> is not used in a dependent context, so the
  123. compiler will look for a name declared at the enclosing namespace scope
  124. (which is the global scope here). It will not look into the base class,
  125. since that is dependent and you may declare specializations of
  126. <code>Base</code> even after declaring <code>Derived</code>, so the compiler cannot
  127. really know what <code>i</code> would refer to. If there is no global
  128. variable <code>i</code>, then you will get an error message.
  129. </p>
  130. <p>In order to make it clear that you want the member of the base class,
  131. you need to defer lookup until instantiation time, at which the base
  132. class is known. For this, you need to access <code>i</code> in a dependent
  133. context, by either using <code>this-&gt;i</code> (remember that <code>this</code> is of
  134. type <code>Derived&lt;T&gt;*</code>, so is obviously dependent), or using
  135. <code>Base&lt;T&gt;::i</code>. Alternatively, <code>Base&lt;T&gt;::i</code> might be brought
  136. into scope by a <code>using</code>-declaration.
  137. </p>
  138. <p>Another, similar example involves calling member functions of a base
  139. class:
  140. </p>
  141. <div class="smallexample">
  142. <pre class="smallexample"> template &lt;typename T&gt; struct Base {
  143. int f();
  144. };
  145. template &lt;typename T&gt; struct Derived : Base&lt;T&gt; {
  146. int g() { return f(); };
  147. };
  148. </pre></div>
  149. <p>Again, the call to <code>f()</code> is not dependent on template arguments
  150. (there are no arguments that depend on the type <code>T</code>, and it is also
  151. not otherwise specified that the call should be in a dependent context).
  152. Thus a global declaration of such a function must be available, since
  153. the one in the base class is not visible until instantiation time. The
  154. compiler will consequently produce the following error message:
  155. </p>
  156. <div class="smallexample">
  157. <pre class="smallexample"> x.cc: In member function `int Derived&lt;T&gt;::g()':
  158. x.cc:6: error: there are no arguments to `f' that depend on a template
  159. parameter, so a declaration of `f' must be available
  160. x.cc:6: error: (if you use `-fpermissive', G++ will accept your code, but
  161. allowing the use of an undeclared name is deprecated)
  162. </pre></div>
  163. <p>To make the code valid either use <code>this-&gt;f()</code>, or
  164. <code>Base&lt;T&gt;::f()</code>. Using the <samp>-fpermissive</samp> flag will also let
  165. the compiler accept the code, by marking all function calls for which no
  166. declaration is visible at the time of definition of the template for
  167. later lookup at instantiation time, as if it were a dependent call.
  168. We do not recommend using <samp>-fpermissive</samp> to work around invalid
  169. code, and it will also only catch cases where functions in base classes
  170. are called, not where variables in base classes are used (as in the
  171. example above).
  172. </p>
  173. <p>Note that some compilers (including G++ versions prior to 3.4) get these
  174. examples wrong and accept above code without an error. Those compilers
  175. do not implement two-stage name lookup correctly.
  176. </p>
  177. <div class="footnote">
  178. <hr>
  179. <h4 class="footnotes-heading">Footnotes</h4>
  180. <h3><a name="FOOT5" href="#DOCF5">(5)</a></h3>
  181. <p>The C++ standard just uses the
  182. term &ldquo;dependent&rdquo; for names that depend on the type or value of
  183. template parameters. This shorter term will also be used in the rest of
  184. this section.</p>
  185. </div>
  186. <hr>
  187. <div class="header">
  188. <p>
  189. Next: <a href="Temporaries.html#Temporaries" accesskey="n" rel="next">Temporaries</a>, Previous: <a href="Static-Definitions.html#Static-Definitions" accesskey="p" rel="prev">Static Definitions</a>, Up: <a href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings" accesskey="u" rel="up">C++ Misunderstandings</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  190. </div>
  191. </body>
  192. </html>