Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Iterator and [] operator

49 views
Skip to first unread message

Jens Thoms Toerring

unread,
Sep 16, 2015, 5:33:28 PM9/16/15
to
Hi,

I've been playing around a bit wist custom random-acccess
iterators (just for my own education and the fun of it) and
read that such an iterator must have an offset dereference
operator ([]). Does that imply that I have to define such an
operator method in the iterator? Looking at what happens when
I have e.g.

Custom_Container cc;
Custom_Container< int >::iterator ccit = cc.begin();

and later, once the container has acquired some elements, doing
e.g.

std::cout << ccit[ 1 ] << std::endl;

I found that an overloaded '[]' operator in the iterator
neither seemed to be necessary nor was it called if defined.
Since 'x[n]' is just syntactic sugar for '*(x+n)' I'm tempted
to assume that having the required '+' and the dereferencing
operator is all that's actually needed. Looking at several
examples of implementations of custom iterators by others (of
which I can't assess their quality) I find that some define a
'[]' operator and others don't, leaving me a bit puzzled what
is required. If it's defined it usually boils down to either

T & operator [ ] ( int n ) { return m_ptr[ n ]; }
or
T & operator [ ] ( int n ) { return *( m_ptr + n ); }

which looks a bit superfluous. Or was having such an overload
somthing required in pre-C++11 days but is now not needed any-
more?
Thanks and best regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Richard

unread,
Sep 16, 2015, 5:41:30 PM9/16/15
to
[Please do not mail me a copy of your followup]

j...@toerring.de (Jens Thoms Toerring) spake the secret code
<d5u5gc...@mid.uni-berlin.de> thusly:

> T & operator [ ] ( int n ) { return m_ptr[ n ]; }
>or
> T & operator [ ] ( int n ) { return *( m_ptr + n ); }
>
>which looks a bit superfluous. Or was having such an overload
>somthing required in pre-C++11 days but is now not needed any-
>more?

IMO, it's nice to explicitly declare operator[] in the header so that
the header more clearly reveals it's intention that this is a random
access iterator.

At least until we get Concepts where you can explicitly declare
yourself as an instance of the RandomAccessIterator concept.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Jens Thoms Toerring

unread,
Sep 16, 2015, 6:38:55 PM9/16/15
to
Richard <legaliz...@mail.xmission.com> wrote:
> [Please do not mail me a copy of your followup]

> j...@toerring.de (Jens Thoms Toerring) spake the secret code
> <d5u5gc...@mid.uni-berlin.de> thusly:

> > T & operator [ ] ( int n ) { return m_ptr[ n ]; }
> >or
> > T & operator [ ] ( int n ) { return *( m_ptr + n ); }
> >
> >which looks a bit superfluous. Or was having such an overload
> >somthing required in pre-C++11 days but is now not needed any-
> >more?

> IMO, it's nice to explicitly declare operator[] in the header so that
> the header more clearly reveals it's intention that this is a random
> access iterator.

May I conclude that it's not required to define a '[]' operator
and that it's a 'courtesy thing' to do it anyway? I'd not mind
defining one at all, but I'm puzzled if it is required and, if
yes, why;-)

mark

unread,
Sep 16, 2015, 8:10:27 PM9/16/15
to
On 2015-09-16 23:33, Jens Thoms Toerring wrote:
> I found that an overloaded '[]' operator in the iterator
> neither seemed to be necessary nor was it called if defined.

It normally is called. You should post complete code.

> Since 'x[n]' is just syntactic sugar for '*(x+n)' I'm tempted
> to assume that having the required '+' and the dereferencing
> operator is all that's actually needed.

'operator*()' doesn't work for this.

You probably have a conversion operator defined - e.g. an 'operator
int*()'. This guy could be used for both 'x[n]' and '*(x+n)' and is
probably invoked.

You should really post complete code.

Öö Tiib

unread,
Sep 17, 2015, 6:15:35 AM9/17/15
to
On Thursday, 17 September 2015 01:38:55 UTC+3, Jens Thoms Toerring wrote:
> Richard <legaliz...@mail.xmission.com> wrote:
> > [Please do not mail me a copy of your followup]
>
> > j...@toerring.de (Jens Thoms Toerring) spake the secret code
> > <d5u5gc...@mid.uni-berlin.de> thusly:
>
> > > T & operator [ ] ( int n ) { return m_ptr[ n ]; }
> > >or
> > > T & operator [ ] ( int n ) { return *( m_ptr + n ); }
> > >
> > >which looks a bit superfluous. Or was having such an overload
> > >somthing required in pre-C++11 days but is now not needed any-
> > >more?
>
> > IMO, it's nice to explicitly declare operator[] in the header so that
> > the header more clearly reveals it's intention that this is a random
> > access iterator.
>
> May I conclude that it's not required to define a '[]' operator
> and that it's a 'courtesy thing' to do it anyway? I'd not mind
> defining one at all, but I'm puzzled if it is required and, if
> yes, why;-)

It is operator that has been required from random access iterator
since C++98. Iterators were in C++ since standardization and
were considered as replacement to raw pointers.
Implementations may still use raw pointers as iterators of
'std::string', 'std::valarray', 'std::array' or 'std::vector' (but no
implementation I know of does).

Code that uses raw pointers as random access iterators of some
raw buffer sometimes uses also operator []. Operator [] was
required to simplify migration of such code to (potentially safer)
containers and iterators. Does that answer your "why"?

Jens Thoms Toerring

unread,
Sep 17, 2015, 7:53:53 AM9/17/15
to
mark <ma...@invalid.invalid> wrote:
> On 2015-09-16 23:33, Jens Thoms Toerring wrote:
> > I found that an overloaded '[]' operator in the iterator
> > neither seemed to be necessary nor was it called if defined.

> It normally is called. You should post complete code.

Sorry, I should have - I just hoped I could do without it
since it's over 250 lines. But if I had you (and some
others) probably would have spotted my mistake immedi-
ately. For completeness sake you can see it at:

http://users.physik.fu-berlin.de/~jtt/iter_test.cpp

The solution to the puzzle is simple: I made the mistake of
using '[]' with a std::reverse_iterator<My_Iter> instead of
with an instance of My_Iter itself. And reverse_iterator, of
course, comes with a '[]' operator of its own. If I try to
use '[]' on an instance of my iterator instead and don't
have '[]' defined in that class, compilation fails as it
should. Sorry for the confusion!

> > Since 'x[n]' is just syntactic sugar for '*(x+n)' I'm tempted
> > to assume that having the required '+' and the dereferencing
> > operator is all that's actually needed.

> 'operator*()' doesn't work for this.

Ok. That was born out of my confusion why it seemed to work
without a '[]' operator...

Jens Thoms Toerring

unread,
Sep 17, 2015, 7:59:10 AM9/17/15
to
Öö Tiib <oot...@hot.ee> wrote:
> It is operator that has been required from random access iterator
> since C++98. Iterators were in C++ since standardization and
> were considered as replacement to raw pointers.
> Implementations may still use raw pointers as iterators of
> 'std::string', 'std::valarray', 'std::array' or 'std::vector' (but no
> implementation I know of does).

> Code that uses raw pointers as random access iterators of some
> raw buffer sometimes uses also operator []. Operator [] was
> required to simplify migration of such code to (potentially safer)
> containers and iterators. Does that answer your "why"?

Yes, thank you! After I finally spotted the mistake I made
in my program (see my reply to 'mark') it's now all clear
- what threw me off (and made me jump to wrong conclusions)
was that, due to an error of mine, it seemed to work with-
out '[]'. But, of course, it doesn't.
0 new messages