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