Derived template classes

T

Torsten Wiebesiek

Hi folks,

I have have upgraded my compiler (gcc 3.3 -> 3.4) and the new version
performs two-stage (or dependent) name lookup:

http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html

Given a templated base class:

template <class T> struct Base
{
int f();
int i;
};

Following derived class doesn't compile anymore:

template <class T> struct Derived : public Base<T>
{
int g() { return i; }
int h() { return f();}
};


The correct implementation is:

template <class T> struct Derived : public Base<T>
{
int g() { return Base<T>::i; }
int h() { return Base<T>::f();}
};

This looks quite confusing, especially when you combine more members of the
base class in an expression.

The gcc docs suggest: "Alternatively, Base<T>::i might be brought into scope
by a using-declaration."

And here is my problem: what is the correct syntax of this using
declaration?

"using Base<T>::i;" doesn't work?

Regards,
Torsten
 
V

Victor Bazarov

Torsten said:
I have have upgraded my compiler (gcc 3.3 -> 3.4) and the new version
performs two-stage (or dependent) name lookup:

http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html

Given a templated base class:

template <class T> struct Base
{
int f();
int i;
};

Following derived class doesn't compile anymore:

template <class T> struct Derived : public Base<T>
{
int g() { return i; }
int h() { return f();}
};


The correct implementation is:

template <class T> struct Derived : public Base<T>
{
int g() { return Base<T>::i; }
int h() { return Base<T>::f();}

Or

int g() { return this->i; }
int h() { return this->f(); }
};

This looks quite confusing, especially when you combine more members of the
base class in an expression.

Yes, it can look confusing.
The gcc docs suggest: "Alternatively, Base<T>::i might be brought into scope
by a using-declaration."

And here is my problem: what is the correct syntax of this using
declaration?

"using Base<T>::i;" doesn't work?

Is that a question? I mean, doesn't it? Please post the code in
its non-working shape. I got this, and it should be working fine:

template <class T> struct Base
{
int f();
int i;
};

template <class T> struct Derived : public Base<T>
{
using Base<T>::i;
using Base<T>::f;
int g() { return i; }
int h() { return f();}
};

int main()
{
Derived<int> di;
di.g();
di.h();
}

V
 
M

mlimber

Torsten said:
Hi folks,

I have have upgraded my compiler (gcc 3.3 -> 3.4) and the new version
performs two-stage (or dependent) name lookup:

http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html

Given a templated base class:

template <class T> struct Base
{
int f();
int i;
};

Following derived class doesn't compile anymore:

template <class T> struct Derived : public Base<T>
{
int g() { return i; }
int h() { return f();}
};


The correct implementation is:

template <class T> struct Derived : public Base<T>
{
int g() { return Base<T>::i; }
int h() { return Base<T>::f();}
};

This looks quite confusing, especially when you combine more members of the
base class in an expression.

The gcc docs suggest: "Alternatively, Base<T>::i might be brought into scope
by a using-declaration."

And here is my problem: what is the correct syntax of this using
declaration?

"using Base<T>::i;" doesn't work?

That should work at function or file scope, not class scope. Are you
sure it's in the right place? You could also do:

int g() { return this->i; }

Cheers! --M
 
M

mlimber

mlimber said:
That should work at function or file scope, not class scope. Are you
sure it's in the right place? You could also do:

int g() { return this->i; }

Cheers! --M

Retraction: Victor is right. It should work at class scope. Post the
code.

Cheers! --M
 
T

Torsten Wiebesiek

Victor said:
Is that a question? I mean, doesn't it? Please post the code in
its non-working shape.

Sorry, you're right. The wrong code is in the function definition:

template <class T>
int Derived<T>::g()
{
using Base<T>::i;
return i;
}

template <class T>
int Derived<T>::h()
{
using Base<T>::f;
return f();
}
[snip]

template <class T> struct Derived : public Base<T>
{
using Base<T>::i;
using Base<T>::f;
int g() { return i; }
int h() { return f();}
};

[snip]

I thought, the using declaration might work at function scope, but somehow
it isn't. At class scope it works fine. :)

Thanks for your help, Victor.

Torsten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top