Google Группы больше не поддерживают новые публикации и подписки в сети Usenet. Опубликованный ранее контент останется доступен.

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

3 просмотра
Перейти к первому непрочитанному сообщению

Rui Maciel

не прочитано,
24 авг. 2004 г., 12:41:0824.08.2004
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

не прочитано,
24 авг. 2004 г., 13:32:3724.08.2004
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

не прочитано,
25 авг. 2004 г., 08:40:5125.08.2004
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

не прочитано,
25 авг. 2004 г., 09:51:5225.08.2004
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 новых сообщений