From -233458065538998026 X-Google-Thread: 7894ca11fe,d47b345505a72a70 X-Google-Attributes: gid7894ca11fe,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!newsfeed.stanford.edu!news.kjsl.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail From: Howard Hinnant Newsgroups: comp.std.c++ Subject: Re: for-else: adding else to for loops Date: Wed, 12 Aug 2009 11:56:15 CST Organization: http://groups.google.com Lines: 63 Sender: cppmods@cs.rpi.edu Approved: austern@google.com Message-ID: <40db48b4-ac8b-42bf-9059-d45abe8981a3@r24g2000vbn.googlegroups.com> References: <5d82cd6c-2a22-4dd7-8c7d-790b579533ea@d15g2000prc.googlegroups.com> <738e6f62-3d38-45f1-8ec1-a91cb8ee5721@m20g2000vbp.googlegroups.com> <60e2f7a6-94b4-4c07-b4d6-88f0472a5eaf@c34g2000yqi.googlegroups.com> NNTP-Posting-Host: netlab.cs.rpi.edu Content-Type: text/plain; charset=ISO-8859-1 To: (Usenet) Return-Path: X-Original-Date: Wed, 12 Aug 2009 05:53:20 -0700 (PDT) X-Submission-Address: std-c++@netlab.cs.rpi.edu Xref: g2news2.google.com comp.std.c++:1270 On Aug 12, 3:53 am, Hakusa wrote: > On Aug 9, 11:51 am, darkmx wrote: > > > for (auto i : seq) { > > for one { cout << "The only number is " << i << '\n'; } > > for first { cout << "The numbers are " << i; } > > for last { cout << " and " << i << '\n'; } > > for others { cout << ", " << i; }} > > > for none {cout << "There are no numbers\n";} > > Wouldn't this require the reservation of one first last others and > none? Or would they become contextual words (which i think would be > confusing). I would think these would have to be contextual keywords. And I've been told contextual keywords are not going to happen in C++ (ever). Another possibility would be to create new keywords such as for_one. > I'm thinking the pseudo code for for last might look like this: > auto it = i; > if( ++it == seq.end() ) > // do block > and that means input iterators couldn't have a for last? Input iterators could not be used with "for one", nor "for last". Both of these require that one knows the end of the sequence is about to happen, but hasn't happened yet. > Honestly, to circumvent this kind of problem, i sometimes use an if > mixed with a do while. > > if( i != end ) { > on_first(); > do some_func( *(i++) ); > while( i != end ); > on_last(); > > } else { > oh_noes(); > } > > I've written prettier code, and it's a little more complex when you > put in the for first, for last, etc equivalents, but it works. I don't > like the new for loop anyway. Without the existence of the "for each" syntax I see no motivation for this at all. With "for each" I can see the motivation. The whole idea of "for each" is to remove the possibility of introducing iteration errors for common loops. Needing to special case loops of length 0, 1, the first element, and/or the last element are arguably sufficiently common as to warrant treatment within this context (not that it should be done for C++0x, it should not -- maybe next time). -Howard -- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]