From -2149306994529748207
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,7eaa8faac8da5016
X-Google-Attributes: gidf78e5,public
From: Pete Becker <petebecker@acm.org>
Subject: Re: Defect Report: member == in container iterators considered harmful
Date: 1999/08/19
Message-ID: <37BB70ED.5895619C@acm.org>#1/1
X-Deja-AN: 514751373
X-NNTP-Posting-Host: 24.128.201.83
Content-Transfer-Encoding: 7bit
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> <user-1808990114430001@aus-as5-186.io.com>
X-Accept-Language: en
X-UID: 0000000001
X-Status: $$$T
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: abuse@mediaone.net
X-Trace: wbnws01.ne.mediaone.net 935031014 24.128.201.83 (Wed, 18 Aug 1999 22:50:14 EDT)
Organization: Road Runner
Mime-Version: 1.0
NNTP-Posting-Date: Wed, 18 Aug 1999 22:50:14 EDT
Newsgroups: comp.std.c++
Originator: clamage@taumet


blargg wrote:
> 
> In article <37B9A195.B7090C3C@acm.org>, Pete Becker <petebecker@acm.org> wrote:
> 
> > 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?
> 

Of course I would. Pointers are usable as STL iterators by necessity,
but it doesn't follow that everything you can do with a pointer is also
appropriate for an iterator.

> > 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?
> 

Constness admittedly doesn't fit well in STL -- it was added in after
the fact. But hacking around with modifications to operator== almost
certainly isn't the right solution. So I'll repeat my question, which
nobody so far has tried to address except by one-liners:

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?

-- 
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]




