From 3603290070289094322
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,7eaa8faac8da5016
X-Google-Attributes: gidf78e5,public
From: postmast.root.admi.gov@iname.com (blargg)
Subject: Re: Defect Report: member == in container iterators considered harmful
Date: 1999/08/18
Message-ID: <user-1808990114430001@aus-as5-186.io.com>#1/1
X-Deja-AN: 514104403
X-NNTP-Posting-Host: aus-as5-186.io.com
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <7p4nj8$n3u@enews4.newsguy.com> <37B98C2E.E3A1EA71@acm.org> <37B98D53.3B7@wanadoo.fr> <37B9A195.B7090C3C@acm.org>
X-UID: 0000000001
X-Status: $$$T
X-Complaints-To: abuse@io.com
X-Trace: hiram.io.com 934956609 16068 208.2.105.186 (18 Aug 1999 06:10:09 GMT)
Organization: e-mail address IS valid - but fools the spammers
NNTP-Posting-Date: 18 Aug 1999 06:10:09 GMT
Newsgroups: comp.std.c++
Originator: clamage@taumet


In article <37B9A195.B7090C3C@acm.org>, Pete Becker <petebecker@acm.org> wrote:

> Valentin Bonnard wrote:
> > 
> > Pete Becker wrote:
> > 
> > > Darin Adler wrote:
> > 
> > > >     bool check_equal(std::deque<int>::iterator i,
> > > >         std::deque<int>::const_iterator ci)
> > > >     {
> > > >         return i == ci;
> > > >     }
> > > >
> > >
> > > Agreed. But the begin and end iterators that you get from a container
> > > are of the same type, and they work just fine for the standard STL
> > > idioms. Why do you need to do this? What coding practices does it
> > > support?
> > 
> > Would you consider the following unatural or silly ?
> > 
> > for (const_iterator it = begin(); it!=end(); ++it)
> >     foo (*it);
> 
> Absolutely. <g> The standard STL idiom is to put the loop in a template
> function.

Yeah. It's simpler for compiler writers to only allow one for loop per
function :-)

Ugh. What a mess, writing multiple functions like this. I'll pass.

> That way you don't have to worry about getting the type of the
> iterator right. But the mistake in that particular loop is the decision
> to use a const_iterator without regard to the actual constness of the
> container.

I suppose if you were given a pointer-to-non-const, you wouldn't consider
ever converting that to a pointer-to-const?

> Write it this way:
> 
> for (Container::iterator it = cont.begin(); it != cont.end(); ++it)
>         foo(*it);

What if we only want to inspect the container, and enforce this decision
by using a const iterator?


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]




