C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Type names defined in base template class not visible in der

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
James Ying
Guest





PostPosted: Tue Oct 28, 2003 5:55 pm    Post subject: Type names defined in base template class not visible in der Reply with quote



Following is the complete code demonstrating the issue:
=====================
Quote:
cat tt.cc
#include <iostream


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

Back to top
Andrey Tarasevich
Guest





PostPosted: Tue Oct 28, 2003 6:26 pm    Post subject: Re: Type names defined in base template class not visible in Reply with quote



James Ying wrote:

Quote:
Following is the complete code demonstrating the issue:
=====================
cat tt.cc
#include <iostream

template class Base {
public:
typedef T difference_type;
};

template class Derived : public Base public:
void test() {
difference_type t = 10;
std::cout << "t is " << t << std::endl;
}
};

int main()
{

Derived 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;
^

Quote:
template<class Cont, class Iter = typename Cont::iterator
class Check_iter : public iterator_traits // ....
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.

Quote:
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.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


Back to top
James Ying
Guest





PostPosted: Tue Oct 28, 2003 11:36 pm    Post subject: Re: Type names defined in base template class not visible in Reply with quote



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.


Andrey Tarasevich <andreytarasevich (AT) hotmail (DOT) com> wrote

Quote:
James Ying wrote:

Following is the complete code demonstrating the issue:
=====================
cat tt.cc
#include <iostream

template class Base {
public:
typedef T difference_type;
};

template class Derived : public Base public:
void test() {
difference_type t = 10;
std::cout << "t is " << t << std::endl;
}
};

int main()
{

Derived 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 // ....
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.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.