From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e95e8407f65e1cfb X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-06-19 13:02:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed-east.nntpserver.com!nntpserver.com!news-out.visi.com!hermes.visi.com!uunet!ash.uu.net!spool0901.news.uu.net!spool0900.news.uu.net!reader0901.news.uu.net!not-for-mail Message-ID: <3D10E366.2010303@mail.com> Date: Wed, 19 Jun 2002 16:02:46 -0400 From: Hyman Rosen User-Agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Look what I caught! was re:Ada paper critic References: <3d0e5750_2@news.bluewin.ch> <3d0fb5eb_3@news.bluewin.ch> <3D10952F.17A62CCF@despammed.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Organization: KBC Financial Products Cache-Post-Path: master.nyc.kbcfp.com!unknown@mosquito.nyc.kbcfp.com X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) NNTP-Posting-Host: 204.253.250.10 X-Trace: 1024516916 reader1.ash.ops.us.uu.net 600 204.253.250.10 Xref: archiver1.google.com comp.lang.ada:26431 Date: 2002-06-19T16:02:46-04:00 List-Id: Robert A Duff wrote: > "Frank J. Lhota" writes: >>Also, the standard only requires a valid pointer to the point just after the >>end of an array; no similar rule applies to the beginning of the array. For >>example, if you declare >> PLANET *before_mercury = solar_system - 1; >>then the standard does NOT require that before_mercury compare meaningfully >>with the addresses of the solar_system elements. > > Really? I thought the above was OK. The STL of C++ depends heavily on > this idiom. Is this a difference between C and C++? If so, what's the > rationale (in C)? It's not OK, and the STL never uses this idiom. The STL uses half-open intervals [begin,end) where the begin iterator points to the start of the sequence being manipulated, and the end iterator points to one past the end of the sequence being manipulated, and the canonical STL algorithm loop is while (begin != end) { operate_on(*begin++); } The "one past the end" pointer can be logical instead of physical, depending on the underlying container, but the algorithms don't care. For example, suppose I wish to copy a sequnce of integers from standard input into a vector. I can do it this way #include #include #include #include using namespace std; int main() { vector v; copy( /* begin */ istream_iterator(cin), /* end */ istream_iterator(), /* to */ back_insert_iterator(v) ); } The istream_iterator() reprsents a logical past-the-end iterator for reading from an input stream. A real input stream iterator will compare equal to that one once it has tried to read past the end-of-file on the input stream. The code one writes for something like copy (copy itself is part of the standard library) is completely ignorant of these shenaningans. It looks like a simple pointer loop - template OI copy(II begin, II end, OI to) { while (begin != end) *to++ = *begin++; return to; } The various iterator types have overloaded operators which implement their behavior. For example, incrementing an istream_iterator causes it to read the next object from the input stream. Dereferencing the iterator returns that object.