Typedef-ing an iterator

J

James Aguilar

Hey all,

I ran into an interesting little problem with defining an iterator in a
custom array class the other day. It goes something like this:

template <typename T>
class Array
{
public:
typedef T * iterator;

iterator begin();
...
};

template <typename T>
Array<T>::iterator Array<T>::begin()
{...}

I thought that this would be enough to get it to compile, but it said that
Array<T>::iterator was an implicit typename declaration and that that was
deprecated. So, my question is, how can I make this work without adding
typename to the return type of every custom typedef in my class (without
actually writing the method inside the class definition, please). Also,
more importantly, I think, what exactly is the problem with doing what I do
in the code above? It seems like Array<T>::iterator is pretty explicit as
to what it's referring to.

James
 
R

Rob Williscroft

James Aguilar wrote in in
comp.lang.c++:
Hey all,

I ran into an interesting little problem with defining an iterator in
a custom array class the other day. It goes something like this:

template <typename T>
class Array
{
public:
typedef T * iterator;

iterator begin();
...
};

template <typename T>
Array<T>::iterator Array<T>::begin()
{...}

I thought that this would be enough to get it to compile, but it said
that Array<T>::iterator was an implicit typename declaration and that
that was deprecated. So, my question is, how can I make this work
without adding typename to the return type of every custom typedef in
my class (without actually writing the method inside the class
definition, please).

You can't.
Also, more importantly, I think, what exactly is
the problem with doing what I do in the code above? It seems like
Array<T>::iterator is pretty explicit as to what it's referring to.

You may think that but a C++ parser isn't as clever as you are.

Maybe it could be made clever enough but that would add serveral
pages to an already 700+ page C++ Standard and a considerable amount
of complexity. Note that it is only reciently that we have had
access to compilers that get anywhere near to 100% conformance to
the Standard and that Standard is nearly 7 years old now.

If you want to discuss this more the place to do it is:


As that is where the "Why the language is this way" questions
should be discussed.

HTH.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top