<p>The following code doesn't compile with gcc, but does with Visual Studio:</p>
<pre><code>template &lt;typename T&gt; class A {
public:
    T foo;
};

template &lt;typename T&gt; class B: public A &lt;T&gt; {
public:
    void bar() { cout &lt;&lt; foo &lt;&lt; endl; }
};
</code></pre>
<p>I get the error:</p>
<blockquote>
<p>test.cpp: In member function ‘void B::bar()’:</p>
<p>test.cpp:11: error: ‘foo’ was not declared in this scope</p>
</blockquote>
<p>But it should be! If I change <code>bar</code> to</p>
<pre><code>void bar() { cout &lt;&lt; this-&gt;foo &lt;&lt; endl; }
</code></pre>
<p>then it <em>does</em> compile, but I don't think I have to do this. Is there something in the official specs of C++ that GCC is following here, or is it just a quirk?</p>

[{'answer_id': 11414, 'author': 'David Joyner', 'author_id': 1146, 'author_profile': 'https://Stackoverflow.com/users/1146', 'pm_score': 5, 'selected': True, 'text': '<p>This changed in <a href="http://gcc.gnu.org/gcc-3.4/changes.html#cplusplus" rel="noreferrer">gcc-3.4</a>.  The C++ parser got much more strict in that release -- per the spec but still kinda annoying for people with legacy or multi-platform code bases.</p>\n'}, {'answer_id': 11425, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 4, 'selected': False, 'text': '<p>Wow.  C++ never ceases to surprise me with its weirdness.</p>\n\n<blockquote>\n  <p>In a template definition, unqualified names will no longer find members of a dependent base (as specified by [temp.dep]/3 in the C++ standard). For example,</p>\n</blockquote>\n\n<pre><code>template &lt;typename T&gt; struct B {\n  int m;\n  int n;\n  int f ();\n  int g ();\n};\nint n;\nint g ();\ntemplate &lt;typename T&gt; struct C : B&lt;T&gt; {\n  void h ()\n  {\n    m = 0; // error\n    f ();  // error\n    n = 0; // ::n is modified\n    g ();  // ::g is called\n  }\n};\n</code></pre>\n\n<blockquote>\n  <p>You must make the names dependent, e.g. by prefixing them with this->. Here is the corrected definition of C::h,</p>\n</blockquote>\n\n<pre><code>template &lt;typename T&gt; void C&lt;T&gt;::h ()\n{\n  this-&gt;m = 0;\n  this-&gt;f ();\n  this-&gt;n = 0\n  this-&gt;g ();\n}\n</code></pre>\n\n<blockquote>\n  <p>As an alternative solution (unfortunately not backwards compatible with GCC 3.3), you may use using declarations instead of this->:</p>\n</blockquote>\n\n<pre><code>template &lt;typename T&gt; struct C : B&lt;T&gt; {\n  using B&lt;T&gt;::m;\n  using B&lt;T&gt;::f;\n  using B&lt;T&gt;::n;\n  using B&lt;T&gt;::g;\n  void h ()\n  {\n    m = 0;\n    f ();\n    n = 0;\n    g ();\n  }\n};\n</code></pre>\n\n<p>That\'s just all kinds of crazy.  Thanks, David.</p>\n\n<p>Here\'s the "temp.dep/3" section of the standard [ISO/IEC 14882:2003] that they are referring to:</p>\n\n<blockquote>\n  <p>In the definition of a class template or a member of a class template, if a base class of the class template depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member. [Example: </p>\n</blockquote>\n\n<pre><code>typedef double A; \ntemplate&lt;class T&gt; class B { \n    typedef int A; \n}; \ntemplate&lt;class T&gt; struct X : B&lt;T&gt; { \n    A a; // a has typedouble \n}; \n</code></pre>\n\n<blockquote>\n  <p>The type name <code>A</code> in the definition of <code>X&lt;T&gt;</code> binds to the typedef name defined in the global namespace scope, not to the typedef name defined in the base class <code>B&lt;T&gt;</code>. ] [Example: </p>\n</blockquote>\n\n<pre><code>struct A { \n    struct B { /* ... */ }; \n    int a; \n    int Y; \n}; \nint a; \ntemplate&lt;class T&gt; struct Y : T { \n    struct B { /* ... */ }; \n    B b; //The B defined in Y \n    void f(int i) { a = i; } // ::a \n    Y* p; // Y&lt;T&gt; \n}; \nY&lt;A&gt; ya; \n</code></pre>\n\n<blockquote>\n  <p>The members <code>A::B</code>, <code>A::a</code>, and <code>A::Y</code> of the template argument <code>A</code> do not affect the binding of names in <code>Y&lt;A&gt;</code>. ] </p>\n</blockquote>\n'}, {'answer_id': 11435, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 5, 'selected': False, 'text': '<p>David Joyner had the history, here is the reason.</p>\n\n<p>The problem when compiling <code>B&lt;T&gt;</code> is that its base class <code>A&lt;T&gt;</code> is unknown from the compiler, being a template class, so no way for the compiler to know any members from the base class.</p>\n\n<p>Earlier versions did some inference by actually parsing the base template class, but ISO C++ stated that this inference can lead to conflicts where there should not be.</p>\n\n<p>The solution to reference a base class member in a template is to use <code>this</code> (like you did) or specifically name the base class:</p>\n\n<pre><code>template &lt;typename T&gt; class A {\npublic:\n    T foo;\n};\n\ntemplate &lt;typename T&gt; class B: public A &lt;T&gt; {\npublic:\n    void bar() { cout &lt;&lt; A&lt;T&gt;::foo &lt;&lt; endl; }\n};\n</code></pre>\n\n<p>More information in <a href="http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Name-lookup.html" rel="nofollow noreferrer">gcc manual</a>.</p>\n'}, {'answer_id': 11703, 'author': 'Matt Price', 'author_id': 852, 'author_profile': 'https://Stackoverflow.com/users/852', 'pm_score': 3, 'selected': False, 'text': '<p>The main reason C++ cannot assume anything here is that the base template can be specialized for a type later. Continuing the original example:</p>\n\n<pre><code>template&lt;&gt;\nclass A&lt;int&gt; {};\n\nB&lt;int&gt; x; \nx.bar();//this will fail because there is no member foo in A&lt;int&gt;\n</code></pre>\n'}, {'answer_id': 212084, 'author': 'hschober', 'author_id': 28906, 'author_profile': 'https://Stackoverflow.com/users/28906', 'pm_score': 2, 'selected': False, 'text': "<p>VC doesn't implemented two-phase lookup, while GCC does. So GCC parses templates before they are instantiated and thus finds more errors than VC. \nIn your example, foo is a dependent name, since it depends on 'T'. Unless you tell the compiler where it comes from, it cannot check the validity of the template at all, before you instantiate it. \nThat's why you have to tell the compiler where it comes from. </p>\n"}]
['https://Stackoverflow.com/questions/11405', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/112/']