473,403 Members | 2,359 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,403 software developers and data experts.

Template inheritance and enum values

Mmmh... I was trying some code, and I bumped in a strange behaviour.
The following piece of code compiles normally :
//////// CODE STARTS /////////////////

#include <iostream>

class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

class D : public B {
public :
void f( int = ONE );
};

void
D::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
}

int
main() {
D d;
d.f();
}

//////// CODE ENDS ///////////////////

Then, if I "templatize" the code, nothing works anymore:

//////// CODE STARTS /////////////////

#include <iostream>

template <typename T>
class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

template <typename T>
class D : public B<T> {
public :
void f( int = ONE );
};

template <typename T>
void
D<T>::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
}

int
main() {
D<bool> d;
d.f();
}

//////// CODE ENDS ///////////////////

Of course, I'm missing a trivial and extremely obvious thing (due to
Murphy's Law), but what?

With templates, the compiler thinks that scope changes when passing
from the base class to the derived one. What really puzzles me is that
no error is shown if I don't use them.

Anyway, I think that the ``correct'' behaviour is the one showed with
the non-templatized classes, so how can I change the code of the
second example in order to make it work?

Thanks for any help,
Matteo

PS : I'm using g++ 3.4.3
Jul 22 '05 #1
3 1763
Matteo Settenvini wrote in news:8eb2e74a.0412161457.118bb672
@posting.google.com in comp.lang.c++:
Then, if I "templatize" the code, nothing works anymore:

This is in the FAQ (its a recient change I beleive).
//////// CODE STARTS /////////////////

#include <iostream>

template <typename T>
class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

template <typename T>
class D : public B<T> {
public :
void f( int = ONE );
void f( int = B<T>::ONE );
};

template <typename T>
void
D<T>::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
std::cout << a << ' ' << B<T>::TWO << ' ' << B<T>::THREE << std::endl;
}

int
main() {
D<bool> d;
d.f();
}

//////// CODE ENDS ///////////////////

Of course, I'm missing a trivial and extremely obvious thing (due to
Murphy's Law), but what?


Names that are dependant on template paramiters can't be looked up
when the template is first parsed, so the extra qualification
is needed.

Also avoid, if at all possible, using all UPPERCASE identifiers
for anything but macro's (#define's), its a rule (convention) that
most c++ programmers follow and it will at some point save you from
some very difficult to find and understand bugs.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Matteo Settenvini wrote:
Mmmh... I was trying some code, and I bumped in a strange behaviour.
The following piece of code compiles normally :
//////// CODE STARTS /////////////////

#include <iostream>

class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

class D : public B {
public :
void f( int = ONE );
};

void
D::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
}

int
main() {
D d;
d.f();
}

//////// CODE ENDS ///////////////////

Then, if I "templatize" the code, nothing works anymore:

//////// CODE STARTS /////////////////

#include <iostream>

template <typename T>
class B {
protected :
enum e { ONE = 1, TWO, THREE};
};

template <typename T>
class D : public B<T> {
public :
void f( int = ONE );
'ONE' is not found during name lookup because it's a dependent
name and for templates base class scopes are not searched. You
need to qualify that name:

void f(int = B<T>::ONE );
};

template <typename T>
void
D<T>::f( int a ) {
std::cout << a << ' ' << TWO << ' '
<< THREE << std::endl;
Same here. 'TWO' and 'THREE' won't be found unless you qualify
them.
}

int
main() {
D<bool> d;
d.f();
}

//////// CODE ENDS ///////////////////

Of course, I'm missing a trivial and extremely obvious thing (due to
Murphy's Law), but what?
Base class scope is not searched if it's a dependent type (and in your
case 'Base<T>' depends on 'T'.
With templates, the compiler thinks that scope changes when passing
from the base class to the derived one. What really puzzles me is that
no error is shown if I don't use them.
I don't understand the last sentence. If you don't use what?
Anyway, I think that the ``correct'' behaviour is the one showed with
the non-templatized classes, so how can I change the code of the
second example in order to make it work?


See above.

V
Jul 22 '05 #3


With templates, the compiler thinks that scope changes when passing
from the base class to the derived one. What really puzzles me is that
no error is shown if I don't use them.

I don't understand the last sentence. If you don't use what?


Templates. Sorry, I ain't a native english speaker and sometimes I mess
up the sentences.

Thanks of your answers!
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gianni Mariani | last post by:
I'm trying to make a generic factory that provides a placement new as well as the accompanying destructor. To do a down-cast I used a static_cast but it turns out that this is not always a...
0
by: gao_bolin | last post by:
I have a library that has a class Signal<T> whose data type is templated. The class comes with a whole bunch of functions and objects to do various processing on the signal. What I would like to...
0
by: Michael Andersson | last post by:
Given a set of classes class A { enum [ ID = 0x0001} }; class B { enum [ ID = 0x0002} }; class B { enum [ ID = 0x0004} }; I wish to generate a composite class, perhaps using something like...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
1
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
8
by: vectorizor | last post by:
Hi all, I have a slight problem with the usage of function templates. It shoud be really easy for you guys to explain me what's wrong. I have one base class and 2 derived classes: /*code*/...
7
by: Kevin | last post by:
I'm creating a template to support state machines. In doing so, I need to pass an enumeration for the number of transitions and a non type parameter for the range of the enum (to allow me to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.