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

Question about STL iterator usage

2 views
Skip to first unread message

Colin

unread,
Jul 17, 2006, 3:18:03 PM7/17/06
to
I've noticed that when working with iterators, most people will use
"(*i).memberFunc()" rather than "i->memberFunc()". Is this simply a
stylistic preference or is there a practical reason to use one and not
the other?

Thanks,
Colin


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

P.J. Plauger

unread,
Jul 17, 2006, 4:18:52 PM7/17/06
to
"Colin" <raz...@gmail.com> wrote in message
news:1153075967.1...@p79g2000cwp.googlegroups.com...

> I've noticed that when working with iterators, most people will use
> "(*i).memberFunc()" rather than "i->memberFunc()". Is this simply a
> stylistic preference or is there a practical reason to use one and not
> the other?

Once upon a time, compilers could instantiate all template class
member functions whether or not they were called. So it wasn't
safe to define operator->() for, say, a templatized iterator class
if the elements it designated could be scalars instead of class
objects. Midway through C++ standardization the instantiation rules
made clear that the compiler should not fuss with any member
functions that never got called. It took several years after that
ruling before most compilers fell into line.

Thus, it has always been safe to write (*i).memberFunc() but only
recently safe to write i->memberFunc(). Those of us who have to
live with a broad range of compilers still favor the first form,
out of prudence.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

sy8111...@gmail.com

unread,
Jul 17, 2006, 10:36:18 PM7/17/06
to

Colin wrote:
> > I've noticed that when working with iterators, most people will use
> > "(*i).memberFunc()" rather than "i->memberFunc()". Is this simply a
> > stylistic preference or is there a practical reason to use one and not
> > the other?

I don't think it is real difference there. Probably just because of
individual declination.

Thomas Maeder

unread,
Jul 17, 2006, 10:37:23 PM7/17/06
to

"Colin" <raz...@gmail.com> writes:

> > I've noticed that when working with iterators, most people will use
> > "(*i).memberFunc()" rather than "i->memberFunc()".

"Nobody knows what most C++ programmers do." Bjarne Stroustrup


> > Is this simply a stylistic preference or is there a practical reason
> > to use one and not the other?

In my impression, you are mistaken - most people use the latter
form. But then again: who knows?

Dave Harris

unread,
Jul 18, 2006, 6:58:01 PM7/18/06
to
raz...@gmail.com (Colin) wrote (abridged):

> I've noticed that when working with iterators, most people will use
> "(*i).memberFunc()" rather than "i->memberFunc()". Is this simply a
> stylistic preference or is there a practical reason to use one and not
> the other?

I often see (*i)->memberFunc(), needed when the container contains
pointers, so maybe they just got in the habit of using (*i) with
iterators even if it isn't needed.

-- Dave Harris, Nottingham, UK.

bjarne

unread,
Jul 19, 2006, 6:13:43 PM7/19/06
to

Thomas Maeder wrote:
> "Colin" <raz...@gmail.com> writes:
>
> > > I've noticed that when working with iterators, most people will use
> > > "(*i).memberFunc()" rather than "i->memberFunc()".
>
> "Nobody knows what most C++ programmers do." Bjarne Stroustrup
>
>
> > > Is this simply a stylistic preference or is there a practical reason
> > > to use one and not the other?
>
> In my impression, you are mistaken - most people use the latter
> form. But then again: who knows?

We don't :-) but there is a reason. In the early days of the STL
(pre-1998), iterators didn't support -> so you had to use (*p).f()
rather than p->f(). Once a style gets established, it can linger for a
long time. Also, some people use antique compilers.

Hendrik Schober

unread,
Jul 20, 2006, 12:30:15 PM7/20/06
to
bjarne <bja...@gmail.com> wrote:
> Thomas Maeder wrote:
> > "Colin" <raz...@gmail.com> writes:
> >
> > > > I've noticed that when working with iterators, most people will use
> > > > "(*i).memberFunc()" rather than "i->memberFunc()".
> >
> > "Nobody knows what most C++ programmers do." Bjarne Stroustrup
> >
> >
> > > > Is this simply a stylistic preference or is there a practical reason
> > > > to use one and not the other?
> >
> > In my impression, you are mistaken - most people use the latter
> > form. But then again: who knows?
>
> We don't :-) but there is a reason. In the early days of the STL
> (pre-1998), iterators didn't support -> so you had to use (*p).f()
> rather than p->f(). Once a style gets established, it can linger for a
> long time. Also, some people use antique compilers.

Also, STL iterators don't support ->*, so if you use
member function pointers, you need to dereference
anyway. So you need to use two idioms to access
members...

Schobi

--
Spam...@gmx.de is never read
I'm Schobi at suespammers dot org

"The sarcasm is mightier than the sword."
Eric Jarvis

0 new messages