Template inheritance and enum values

M

Matteo Settenvini

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
 
R

Rob Williscroft

Matteo Settenvini wrote in @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 said:
};

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

std::cout said:
}

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.
 
V

Victor Bazarov

Matteo said:
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 said:
};

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 said:
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
 
M

Matteo Settenvini

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!
 

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
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top