473,387 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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;
Jul 19 '05 #1
2 2828
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

Jul 19 '05 #2
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 <an**************@hotmail.com> wrote in message news:<vp************@news.supernews.com>...
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.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: verec | last post by:
Consider a first version: --- drawable.hpp --- #include "gcdata.hpp" struct drawable { ... virtual int internal_new_GC(gcdata * gcd) = 0 ; } ; --- gcdata.hpp ---
0
by: Arne Claus | last post by:
I've got a tricky problem here. I try to create an object-hierarchie, based on a template base class like template <class T> class Node { addChild(Node<T> *) =0; // the Node-Container wil be...
1
by: sygnosys | last post by:
Hi, I'm using VC++ 2005 can anyone tell me what's wrong with the following piece of code. It simply does not compile spitting out the following error "error C2039: 'TypeDerived' : is not a...
4
by: michael.alexeev | last post by:
Hi all. Consider the following code fragment: #include <string> struct Base{ virtual ~Base() {} }; template <typename T> struct Derived : public Base {}; Base* Create (int param) { switch...
9
by: Mirko Puhic | last post by:
Is there a way to properly do this? struct Derived; struct Base{ Derived der; };
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
19
by: n.torrey.pines | last post by:
I have the following tree definition: template<typename T> struct tree { T first; vector<tree<T second; // branches }; which compiles successfully. What I'd like to do though is to use...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
6
by: Generic Usenet Account | last post by:
I ran a small experiment involving RTTI and dynamic casting. Basically what I did was to cast a base class pointer to a derived type (yes, I know that is not kosher). I then performed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.