1<html lang="en"> 2<head> 3<title>Name lookup - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings" title="C++ Misunderstandings"> 9<link rel="prev" href="Static-Definitions.html#Static-Definitions" title="Static Definitions"> 10<link rel="next" href="Temporaries.html#Temporaries" title="Temporaries"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Name-lookup"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Temporaries.html#Temporaries">Temporaries</a>, 53Previous: <a rel="previous" accesskey="p" href="Static-Definitions.html#Static-Definitions">Static Definitions</a>, 54Up: <a rel="up" accesskey="u" href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings">C++ Misunderstandings</a> 55<hr> 56</div> 57 58<h4 class="subsection">11.8.2 Name lookup, templates, and accessing members of base classes</h4> 59 60<p><a name="index-base-class-members-3333"></a><a name="index-two_002dstage-name-lookup-3334"></a><a name="index-dependent-name-lookup-3335"></a> 61The C++ standard prescribes that all names that are not dependent on 62template parameters are bound to their present definitions when parsing 63a template function or class.<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a> Only names that are dependent are looked up at the point 64of instantiation. For example, consider 65 66<pre class="smallexample"> void foo(double); 67 68 struct A { 69 template <typename T> 70 void f () { 71 foo (1); // <span class="roman">1</span> 72 int i = N; // <span class="roman">2</span> 73 T t; 74 t.bar(); // <span class="roman">3</span> 75 foo (t); // <span class="roman">4</span> 76 } 77 78 static const int N; 79 }; 80</pre> 81 <p>Here, the names <code>foo</code> and <code>N</code> appear in a context that does 82not depend on the type of <code>T</code>. The compiler will thus require that 83they are defined in the context of use in the template, not only before 84the point of instantiation, and will here use <code>::foo(double)</code> and 85<code>A::N</code>, respectively. In particular, it will convert the integer 86value to a <code>double</code> when passing it to <code>::foo(double)</code>. 87 88 <p>Conversely, <code>bar</code> and the call to <code>foo</code> in the fourth marked 89line are used in contexts that do depend on the type of <code>T</code>, so 90they are only looked up at the point of instantiation, and you can 91provide declarations for them after declaring the template, but before 92instantiating it. In particular, if you instantiate <code>A::f<int></code>, 93the last line will call an overloaded <code>::foo(int)</code> if one was 94provided, even if after the declaration of <code>struct A</code>. 95 96 <p>This distinction between lookup of dependent and non-dependent names is 97called two-stage (or dependent) name lookup. G++ implements it 98since version 3.4. 99 100 <p>Two-stage name lookup sometimes leads to situations with behavior 101different from non-template codes. The most common is probably this: 102 103<pre class="smallexample"> template <typename T> struct Base { 104 int i; 105 }; 106 107 template <typename T> struct Derived : public Base<T> { 108 int get_i() { return i; } 109 }; 110</pre> 111 <p>In <code>get_i()</code>, <code>i</code> is not used in a dependent context, so the 112compiler will look for a name declared at the enclosing namespace scope 113(which is the global scope here). It will not look into the base class, 114since that is dependent and you may declare specializations of 115<code>Base</code> even after declaring <code>Derived</code>, so the compiler can't 116really know what <code>i</code> would refer to. If there is no global 117variable <code>i</code>, then you will get an error message. 118 119 <p>In order to make it clear that you want the member of the base class, 120you need to defer lookup until instantiation time, at which the base 121class is known. For this, you need to access <code>i</code> in a dependent 122context, by either using <code>this->i</code> (remember that <code>this</code> is of 123type <code>Derived<T>*</code>, so is obviously dependent), or using 124<code>Base<T>::i</code>. Alternatively, <code>Base<T>::i</code> might be brought 125into scope by a <code>using</code>-declaration. 126 127 <p>Another, similar example involves calling member functions of a base 128class: 129 130<pre class="smallexample"> template <typename T> struct Base { 131 int f(); 132 }; 133 134 template <typename T> struct Derived : Base<T> { 135 int g() { return f(); }; 136 }; 137</pre> 138 <p>Again, the call to <code>f()</code> is not dependent on template arguments 139(there are no arguments that depend on the type <code>T</code>, and it is also 140not otherwise specified that the call should be in a dependent context). 141Thus a global declaration of such a function must be available, since 142the one in the base class is not visible until instantiation time. The 143compiler will consequently produce the following error message: 144 145<pre class="smallexample"> x.cc: In member function `int Derived<T>::g()': 146 x.cc:6: error: there are no arguments to `f' that depend on a template 147 parameter, so a declaration of `f' must be available 148 x.cc:6: error: (if you use `-fpermissive', G++ will accept your code, but 149 allowing the use of an undeclared name is deprecated) 150</pre> 151 <p>To make the code valid either use <code>this->f()</code>, or 152<code>Base<T>::f()</code>. Using the <samp><span class="option">-fpermissive</span></samp> flag will also let 153the compiler accept the code, by marking all function calls for which no 154declaration is visible at the time of definition of the template for 155later lookup at instantiation time, as if it were a dependent call. 156We do not recommend using <samp><span class="option">-fpermissive</span></samp> to work around invalid 157code, and it will also only catch cases where functions in base classes 158are called, not where variables in base classes are used (as in the 159example above). 160 161 <p>Note that some compilers (including G++ versions prior to 3.4) get these 162examples wrong and accept above code without an error. Those compilers 163do not implement two-stage name lookup correctly. 164 165 <div class="footnote"> 166<hr> 167<h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> The C++ standard just uses the 168term “dependent” for names that depend on the type or value of 169template parameters. This shorter term will also be used in the rest of 170this section.</p> 171 172 <hr></div> 173 174 </body></html> 175 176