By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Derived template classes

P: n/a
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
Dec 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Torsten Wiebesiek wrote:
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
Dec 15 '05 #2

P: n/a

Torsten Wiebesiek wrote:
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

Dec 15 '05 #3

P: n/a
mlimber wrote:
Torsten Wiebesiek wrote:
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


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

Cheers! --M

Dec 15 '05 #4

P: n/a
Victor Bazarov wrote:
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.


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
Dec 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.