iterator as a member?

G

Gernot Frisch

Hi,

list<int> ml;

list<int>::iterator it=ml.begin();

why can't I use / why isn't there:

ml::iterator it = ml.begin();

It's pretty annoying having to know the exact type of a variable in
order to create an iterator for it. MFC (which is really, really a lot
of crap) has one unique iterator for all and everything: "POSITION". I
don't need it this way, since there are good reasons for different
iterator classes, but why can't an object of a container class provide
it?

-Gernot
 
J

John Harrison

Gernot Frisch said:
Hi,

list<int> ml;

list<int>::iterator it=ml.begin();

why can't I use / why isn't there:

ml::iterator it = ml.begin();

It's pretty annoying having to know the exact type of a variable in
order to create an iterator for it. MFC (which is really, really a lot
of crap) has one unique iterator for all and everything: "POSITION". I
don't need it this way, since there are good reasons for different
iterator classes, but why can't an object of a container class provide
it?

-Gernot

What you are asking for is something akin to typeof

typeof(ml)::iterator it = ml.begin();

See http://gcc.gnu.org/onlinedocs/gcc/Typeof.html

It's often proposed as an addition to the language. I don't if it is likely
to make it.

john
 
J

Jeff Flinn

Gernot Frisch said:
Hi,

list<int> ml;

list<int>::iterator it=ml.begin();

why can't I use / why isn't there:

ml::iterator it = ml.begin();

It's pretty annoying having to know the exact type of a variable in
order to create an iterator for it. MFC (which is really, really a lot
of crap) has one unique iterator for all and everything: "POSITION". I
don't need it this way, since there are good reasons for different
iterator classes, but why can't an object of a container class provide
it?

Because list<int> is a type and ml is an object. Currently, only limited
type info can be extracted from an object via typeid. As john harrison
mentioned auto is proposed to be added. In the mean time you can somewhat
alleviate the situation by:

1) use typedefs

typedef std::list<int> tL;
typedef tL::iterator tItr;

tL ml;

tItr = ml.begin();

...
2) use standard generic algorithms

std::for_each( ml.begin(), ml.end(), somefnc );
std::copy( ... )
std::transform( ... )

3) make your own generic althorithms.

template< class Itr > SomeFunction( Itr beg, Itr end )
{
for( ; beg != end ; ++beg )
{
...
}
}

Jeff F
 
G

Gernot Frisch

What you are asking for is something akin to typeof
typeof(ml)::iterator it = ml.begin();

See http://gcc.gnu.org/onlinedocs/gcc/Typeof.html

It's often proposed as an addition to the language. I don't if it is likely
to make it.

My compiler does not support it I think... Why can't a container class
have the iterator as a member itself?? It would have been easy to
implement, wouldn't it?
-Gernot
 
J

John Harrison

Gernot Frisch said:
My compiler does not support it I think... Why can't a container class
have the iterator as a member itself?? It would have been easy to
implement, wouldn't it?
-Gernot

The container does have the iterator as a member of itself. That's what you
are accessing when you say

list<int>::iterator

It's just that the particular syntax you are looking for is not supported.
You cannot go from something that is not a type (a variable for instance) to
a type. It's that shortcoming that typeof is meant to address.

john
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top