Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template, typename and vector::iterator: why this error?

3 views
Skip to first unread message

Rui Maciel

unread,
Aug 24, 2004, 12:41:08 PM8/24/04
to
I was trying to write a vector class when I stumbled on an compiler
error. After a quick newsgroup search, I've found a way to fix that
error but still, I don't have a clue about the logic behind the fix.

The error-free code is pasted below.

<declaration>
template <typename T, unsigned int i>
class cVector {
std::vector<T> data;
public:
// snip code
void print();
};
</declaration>

<implementation>
// snip
template <typename T, unsigned int i>
void cVector<T,i>::print(){
typename std::vector<T>::iterator iter; // this line
// snip
}
// snip
</implementation>


The thing is, I thought the right declaration of a iterator type was simply:

std::vector<T>iterator iter;

But the compiler (GCC 3.4.1) throws the following error:

<error message>
In file included from cVector.cpp:2:
cVector.hpp: In member function `void cVector<T, i>::print()':
cVector.hpp:48: error: expected `;' before "iter"
make: *** [cVector.o] Error 1
</error message>


So my question is what's the logic behind the use of the "typename"
keyword? Why is it needed and what does it do in that context?


Thanks in advance
Rui Maciel

Alwyn

unread,
Aug 24, 2004, 1:32:37 PM8/24/04
to
In article <412b6fa9$0$1834$a729...@news.telepac.pt>, Rui Maciel
<rui_m...@mail.com> wrote:

> I was trying to write a vector class when I stumbled on an compiler
> error. After a quick newsgroup search, I've found a way to fix that
> error but still, I don't have a clue about the logic behind the fix.

Basically, it's a kludge, caused by deficiencies in C++ syntax. As I
recall, the keyword was introduced somewhat late in the standardisation
process.

Does this explain it for you?
<http://www.glenmccl.com/ansi_035.htm>


Alwyn

Rui Maciel

unread,
Aug 25, 2004, 8:40:51 AM8/25/04
to
Alwyn wrote:
> Basically, it's a kludge, caused by deficiencies in C++ syntax. As I
> recall, the keyword was introduced somewhat late in the standardisation
> process.
>
> Does this explain it for you?
> <http://www.glenmccl.com/ansi_035.htm>

The link you gave did a nice job explaining the usage of the typename
keyword. Nice one :)

Still, there is something that I don't quite get it. The examples given
in Jonathan Schilling's article explain only the need to use the
typename keyword when addressing a member of a template parameter. The
article explains it really well an so it is easy to understand the need
of it. But in the sample code I posted, the data type which is being
declared isn't a member of the template parameter.

So, why is the typename keyword needed? Is it because the iterator data
type is a member of a class which uses the template datatype?
Nevertheless, it seems to me that the compiler should recognize the
iterator data type as a member of class vector<T> without any problem.


Rui Maciel

Alwyn

unread,
Aug 25, 2004, 9:51:52 AM8/25/04
to
In article <412c88d6$0$20797$a729...@news.telepac.pt>, Rui Maciel
<rui_m...@mail.com> wrote:

> Still, there is something that I don't quite get it. The examples given
> in Jonathan Schilling's article explain only the need to use the
> typename keyword when addressing a member of a template parameter. The
> article explains it really well an so it is easy to understand the need
> of it. But in the sample code I posted, the data type which is being
> declared isn't a member of the template parameter.
>
> So, why is the typename keyword needed? Is it because the iterator data
> type is a member of a class which uses the template datatype?
> Nevertheless, it seems to me that the compiler should recognize the
> iterator data type as a member of class vector<T> without any problem.

You had:


> typename std::vector<T>::iterator iter; // this line

The 'iter' type is an incomplete type and dependent on the template
parameter, no? In theory, the compiler cannot know that
'std::vector<T>::iterator' is the name of a type, hence the need for
the 'typename' keyword.

Now you may argue, and I will agree with you, that in this case, the
compiler is perfectly capable of working out the type from the
definition of std::vector. However, this might not apply to a
particular specialisation of std::vector<>, and until an instantiation
is seen, the compiler cannot determine this.

This is the rule the C++ designers worked out. Like much else, it is
imperfect, but I think we can live with it.


Alwyn

0 new messages