home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,083 of 33,346   
   Dave Abrahams to All   
   Re: Why does range based for loop requir   
   02 Apr 12 10:28:35   
   
   From: dave@boostpro.com   
      
   on Mon Apr 02 2012, Pete Becker  wrote:   
      
   > On 2012-04-01 04:01:53 +0000, Dave Abrahams said:   
   >   
   >> on Wed Mar 28 2012, Pete Becker  wrote:   
   >>   
   >>> On 2012-03-28 02:46:07 +0000, Dave Abrahams said:   
   >>>   
   >>>> on Sat Mar 24 2012, Pete Becker  wrote:   
   >>>>   
   >>>>> On 2012-03-24 05:31:44 +0000, Frank Birbacher said:   
   >>>>>   
   >>>>>>   
   >>>>>> // not allowed in C++11, but I want it to work:   
   >>>>>> int i;   
   >>>>>> for(i : vec) {...}   
   >>>>>>   
   >>>>>   
   >>>>> What should the value of i be after the loop? More generally,   
   >>>>> what do you want to be able to do with i after this loop?   
   >>>>   
   >>>> The value of i is most useful if you break from the loop.   
   >>>> BOOST_FOREACH supports Frank's usage and frankly I'm surprised to   
   >>>> learn that C++11's native construct doesn't.   
   >>>   
   >>> Okay, that's reasonable. But what if you don't exit early? I   
   >>> suppose the value could be the last element, but that's   
   >>> artificial; the "true" value is *end, but end doesn't have to be   
   >>> dereferenceable.   
   >>   
   >> I don't understand why you'd call that "artificial," or on what   
   >> basis you're calling some possibly non-existent value "true."  The   
   >> value of the last element is the only value that could possibly be   
   >> left there, assuming the loop is entered at all.   
   >   
   > "the only value that could possibly be left there" is exactly why   
   > it's artificial. That's implementation showing through an   
   > abstraction.   
      
   I guess we'll just have to disagree on this one.  There's no variation   
   on implementation that could possibly leave a different value in the   
   loop variable.   
      
   > Once the for loop has moved past the end of the sequence, there's no   
   > value remaining.   
      
   You seem to see the value in the loop variable as an abstraction of   
   "the current element."  That is the case when you're in the loop, but   
   of course, not before the loop or after you've left the loop.  Then,   
   it's an abstraction of "the last element seen so far, or the initial   
   value if no element has yet been seen" (which abstraction encompasses   
   "the current element").  Today that distinction is moot because the   
   declaration is required, but if we allow looping over a   
   previously-declared variable, we'll have to expand our notion of what   
   the loop variable means accordingly... or, I suppose, label it as   
   artificial or false.  Personally, I prefer to choose an abstraction   
   that doesn't require such labeling.   
      
   Regards,   
      
   --   
   Dave Abrahams   
   BoostPro Computing   
   http://www.boostpro.com   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca