 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paul Pedriana Guest
|
Posted: Sat Aug 27, 2005 5:55 pm Post subject: Parent template member access illegal in standard C++? |
|
|
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 ]
|
|
| Back to top |
|
 |
sashan Guest
|
Posted: Sun Aug 28, 2005 5:40 pm Post subject: Re: Parent template member access illegal in standard C++? |
|
|
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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Aug 28, 2005 6:39 pm Post subject: Re: Parent template member access illegal in standard C++? |
|
|
Paul Pedriana wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: | -----------------------------------------------------------------------
template <typename T, int i
struct A {
T a;
};
template
struct B : public A
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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|