C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Parent template member access illegal in standard C++?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Paul Pedriana
Guest





PostPosted: Sat Aug 27, 2005 5:55 pm    Post subject: Parent template member access illegal in standard C++? Reply with 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.

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





PostPosted: Sun Aug 28, 2005 5:40 pm    Post subject: Re: Parent template member access illegal in standard C++? Reply with quote



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





PostPosted: Sun Aug 28, 2005 6:39 pm    Post subject: Re: Parent template member access illegal in standard C++? Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.