473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Derived template classes

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
4 1688
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Art | last post by:
This is partially an academic question, but I'm trying to understand templates better. I have a base class that uses template parameters to define the behavior of its class. I want to subclass this...
4
by: Brad Kartchner | last post by:
I'm attempting to write a program with several child classes derived from a single parent class. The parent class has a couple of variables that need to be present in each of the child classes. ...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
8
by: Gonçalo Rodrigues | last post by:
Hi all, I have a template class (call it Object) whose instances have a variable size part - an array of of T objects. But this variable size part is fixed at creation time so instead of...
1
by: Steven T. Hatton | last post by:
I started working on something I had failed to understand several months ago. The original discussion on c.l.c++-m has this message ID: news://Message-ID:...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
9
by: flopbucket | last post by:
Hi, Say I have a baseclass B that has many derived classes, let's say C..Z, for example: class B { }; class C : public B {};
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.