Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Type names defined in base template class not visible in derived class in gcc3.2?

Reply
Thread Tools

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

 
 
James Ying
Guest
Posts: n/a
 
      10-28-2003
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;
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      10-28-2003
James Ying wrote:

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


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.

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

 
Reply With Quote
 
 
 
 
James Ying
Guest
Posts: n/a
 
      10-28-2003
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> James Ying wrote:
>
> > 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):

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
When is a base class protected member not visible in a derived class? Andy Lomax C++ 5 06-30-2005 04:34 AM
Re: template class derived from non-template base class Matt Graham C++ 0 07-21-2003 09:02 PM



Advertisments