Topic: Is the arrow operator defined for iterators


Author: "Kirk" <kirk_korver@hotmail.com>
Date: Fri, 2 Feb 2007 14:25:54 CST
Raw View
Group,

Please help me settle a nagging quesiton I've had. I want to know if
the arrow operator is defined for iterators.

Let me try to explain. Someone told me that the STL does not guarantee
that iterators are actually implemented as pointers. STL requires
iterators to support the star operator. But what about the arrow
operator? In the implementations provided by MSVC, the iterators are
actually pointers so the arrow works, but this is only a side effect
of the implementation, and not a requirement?

I'll try to re-ask so my question is clear.

If we consider using an iterator called it, we must use the   (*it).
syntax as opposed to the it-> syntax.

I don't know where to look for this answer and I really would like to
know what the rules *really* are.

Thank you in advance for help.

--Kirk

Given this class:

struct Item
{
    void foo();
};

and a collection Collection <item> c; Where collection is any of the
standard STL containers.

This works, but looks funy. I was told, this is the ONLY way to write
portable code though.

for (Collection <item>::iterator it = c.begin(); it != c.end(); ++it)
{
      (*it).foo();
}

Is this valid also?

for (Collection <item>::iterator it = c.begin(); it != c.end(); ++it)
{
      it->foo();
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: clarkcox3@gmail.com ("Clark S. Cox III")
Date: Fri, 2 Feb 2007 21:01:46 GMT
Raw View
Kirk wrote:
> Group,
>
> Please help me settle a nagging quesiton I've had. I want to know if
> the arrow operator is defined for iterators.

It is.

>
> Let me try to explain. Someone told me that the STL does not guarantee
> that iterators are actually implemented as pointers.

Correct. In fact, most of them are *not* implemented as pointers, and
would, in fact, be impossible to implement as pointers.

> STL requires
> iterators to support the star operator.

True

> But what about the arrow
> operator?

Yes, they must implement this as well.

> In the implementations provided by MSVC, the iterators are
> actually pointers so the arrow works, but this is only a side effect
> of the implementation, and not a requirement?
>
> I'll try to re-ask so my question is clear.
>
> If we consider using an iterator called it, we must use the   (*it).
> syntax as opposed to the it-> syntax.
>
> I don't know where to look for this answer and I really would like to
> know what the rules *really* are.

The rules are in the Standard itself. Tables 72, 74, and 76 show that
a->m is defined for all input, forward, bidirectional and random access
iterators and is equivalent to (*a).m .


--
Clark S. Cox III
clarkcox3@gmail.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]