Topic: Parent template member access illegal in standard C++?


Author: pedriana@remove_this.pacbell.net (Paul Pedriana)
Date: Sat, 27 Aug 2005 17:55:14 GMT
Raw View
The 'a++' below fails under GCC 3.4 and EDG, but succeeds under other
compilers I've tried (including GCC 2.9x). I'm wondering where in the
C++ standard it specifies that this is illegal, because it's a new one
to me.

Take away the 'int i' template parameter and the compilation succeeds.

-----------------------------------------------------------------------
template <typename T, int i>
struct A {
   T a;
};

template <typename T, int i>
struct B : public A<int, i> {
   void GetA() {
     a++;            // "error: `a' undeclared (first use this function)"
     this->a++;      // OK
     A<int, i>::a++; // OK
   }
};
------------------------------------------------------------------------

Thanks.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "sashan" <sashang@gmail.com>
Date: 28 Aug 2005 17:40:02 GMT
Raw View
I think that GCC 3.4 is right for the following reason. The name
'A<int, i>' is a dependent name because it depends on the the template
parameters (in this case i). The name 'a' is a non-dependent name
because it does not depend on a template parameter. The compiler is
meant to follow the rule 'do not look up non-dependent names in a
dependent base class'. The reason why 'this->a++' compiles is because
the whole name 'this->a' is now a dependent name (because 'this' is
dependent on the template parameter). Removing 'i' from the template
results in A<int> which is a non-dependent name, so the compiler can
resolve the look-up to the base class.

There is more info here
http://www.parashift.com/c++-faq-lite/templates.html

--
sashan
http://sashang.orcon.net.nz/index.htm

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Victor Bazarov" <v.Abazarov@comAcast.net>
Date: Sun, 28 Aug 2005 12:39:40 CST
Raw View
Paul Pedriana wrote:
> The 'a++' below fails under GCC 3.4 and EDG, but succeeds under other
> compilers I've tried (including GCC 2.9x). I'm wondering where in the
> C++ standard it specifies that this is illegal, because it's a new one
> to me.

It is specified in clause 14, section 14.6.2, and has been since 1997.

> Take away the 'int i' template parameter and the compilation succeeds.

Yes, if you remove 'int i', the base class for struct B stops being
a _dependent_type_, so the name of its member ('a' in your case) stop
being a dependent name.  You found two correct solutions for the case:
the name has to be either an elaborate name or an explicit member access
expression.

> -----------------------------------------------------------------------
> template <typename T, int i>
> struct A {
>   T a;
> };
>
> template <typename T, int i>
> struct B : public A<int, i> {
>   void GetA() {
>     a++;            // "error: `a' undeclared (first use this
>     function)" this->a++;      // OK
>     A<int, i>::a++; // OK
>   }
> };
> ------------------------------------------------------------------------

V


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]