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