public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* template member variable bug in gcc 3.4?
@ 2004-04-21 1:01 Dyer, Christopher
2004-04-21 19:33 ` Alexandre Oliva
0 siblings, 1 reply; 3+ messages in thread
From: Dyer, Christopher @ 2004-04-21 1:01 UTC (permalink / raw)
To: gcc-bugs
I've got a question for the c++ experts on this list regarding an unusual coding construct that is failing to compile under gcc 3.4 but works under 3.2.
(Code sample is below). I've got a templatized class that derives from a class templatized on the same type T. This base class defines a protected member variable t_ of the type T*. In the derived class, referencing t_ without any scope produces "error 't_' undeclared (first use this function)" in gcc 3.4, but works just fine in gcc 3.2.
I can work around this by explicitly naming the base class and using the scope resolution operator or by prefacing the member variable name with this->.
Can someone explain to me whether this is a compiler bug or an aspect of C++ syntax that I don't understand?
Thanks for any insight,
Chris
-------------code sample-----------------
class foo {
public:
int position() const { return 1; }
};
template <class T> class driver {
public:
virtual ~driver() { delete t_; }
driver(T *t) : t_(t) {}
protected:
T *t_;
};
template <class T> class wrapper : public driver<T> {
public:
wrapper() : driver<T>(new T()) { }
// the following line compiles in GCC 3.2, but not in GCC 3.4.
int position() const { return t_->position(); };
// to make it work, it must be changed to one of the following
// (compiles in both GCC 3.2 and 3.4)
// int position() const { return driver<T>::t_->position(); };
// int position() const { return this->t_->position(); };
};
int main() {
wrapper<foo> test;
return 0;
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: template member variable bug in gcc 3.4?
2004-04-21 1:01 template member variable bug in gcc 3.4? Dyer, Christopher
@ 2004-04-21 19:33 ` Alexandre Oliva
0 siblings, 0 replies; 3+ messages in thread
From: Alexandre Oliva @ 2004-04-21 19:33 UTC (permalink / raw)
To: Dyer, Christopher; +Cc: gcc-bugs
On Apr 20, 2004, "Dyer, Christopher" <cdyer@amazon.com> wrote:
> // the following line compiles in GCC 3.2, but not in GCC 3.4.
> int position() const { return t_->position(); };
> // to make it work, it must be changed to one of the following
> // (compiles in both GCC 3.2 and 3.4)
> // int position() const { return driver<T>::t_->position(); };
> // int position() const { return this->t_->position(); };
t_ is not a template-dependent name, so it is supposed to bind at
parse time, not at template instantiation time. However, since
there's no declaration for t_ visible at that point, the compiler
rightfully reports an error. Older versions of GCC used to defer
binding to template instantiation time, which was incorrect.
By using this->t_ or driver<T>::t_, you make t_ template-dependent, so
the name is only resolved at template instantiation time.
--
Alexandre Oliva http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist oliva@{lsd.ic.unicamp.br, gnu.org}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: template member variable bug in gcc 3.4?
@ 2004-04-21 1:05 Giovanni Bajo
0 siblings, 0 replies; 3+ messages in thread
From: Giovanni Bajo @ 2004-04-21 1:05 UTC (permalink / raw)
To: cdyer; +Cc: gcc-bugs
Christopher Dyer wrote:
>Can someone explain to me whether this is a compiler bug
> or an aspect of C++ syntax that I don't understand?
This is a FAQ. Please consult http://gcc.gnu.org/gcc-3.4/changes.html.
Giovanni Bajo
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2004-04-21 19:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-21 1:01 template member variable bug in gcc 3.4? Dyer, Christopher
2004-04-21 19:33 ` Alexandre Oliva
2004-04-21 1:05 Giovanni Bajo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).