Type names defined in base template class not visible in derived class in gcc3.2?

J

James Ying

Following is the complete code demonstrating the issue:
=====================
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;
 
A

Andrey Tarasevich

James said:
Following is the complete code demonstrating the issue:
=====================
#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):

I'd say that it should be an error, not just a warning. According to the
definition given in 14.6.2.1/1, name 'difference_type' used in the
definition of method 'test()' is not a dependent name. For this reason,
as 14.6/8 says, this name should be looked up is accordance with usual
name lookup rules at the point of template definition. Since class
template B<T> does not participate in this name lookup process (see
14.6.2/3), the name 'difference_type' will not be found and the code is
ill-formed. That's actually how Comeau's compiler treats this code - it
issues an error

"ComeauTest.c", line 13: error: identifier "difference_type" is undefined
difference_type t = 10;
^
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".

Well, the code in the book can be incomplete or outdated.
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;

That would be the right thing to do.
 
J

James Ying

Thank you for pointing to the right sections in the standard, and I
agree with you. The standard is pretty clean on this, although it
makes life a bit mroe difficult -- the supposed trick presented in
TC+PL about checked iterator will not do the trick to save a few
typings.
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top