=====================
cat tt.cc
#include <iostream>
template <class T>
class Base {
public:
typedef T difference_type;
};
template <class T>
class Derived : public Base<T> {
public:
void test() {
difference_type t = 10;
std::cout << "t is " << t << std::endl;
}
};
int main()
{
Derived<int> d;
d.test();
}
========== end of code ======================
This code compiles without warning in g++2.96, MIPSpro C++ Compilers:
Version 7.3.1.1m, Sun WorkShop 6 update 2 C++ 5.3 Patch 111685-10.
However g++3.2 gives warning: implicit typename is deprecated, please
see the documentation for details.
My question: is the warning necessary according to the C++ standard?
While I haven't read the standard carefully enough to answer the
question definitely, the C++PL book gives a similar code in the class
of Checked Iterator (pp. 562-563, 3rd editon):
template<class Cont, class Iter = typename Cont::iterator>
class Check_iter : public iterator_traits<Iter> {
// ....
reference_type operator*() {
// ...
Note the reference_type (rather, reference) is type-defined in the
base template iterator_traits<Iter>, and used directly in the derived
class without prefixing "typename".
One obvious approach to get rid of the g++ warning is to add the
following code in the derived class:
typedef typename Base<T>::difference_type difference_type;