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,052 of 33,346   
   Dave Harris to Frank Birbacher   
   Re: Why does range based for loop requir   
   26 Mar 12 10:23:05   
   
   From: brangdon@cix.compulink.co.uk   
      
   bloodymir.crap@gmx.net (Frank Birbacher) wrote (abridged):   
   > int i;   
   > for(i : vec) {   
   >    if(pred(i))   
   >      break;   
   > }   
   > // found or not?   
   >   
   > So, is the non-usefulness the rationale behind the design of the   
   > range loop?   
      
   More or less. We could achieve a similar effect to a for-loop by using   
   labels, goto and if-statements; we prefer the for-loop because it is   
   more limited, not because it is more general.   
      
   The more limited loops are not so much about brevity as showing intent.   
   If your example was written:   
      
        bool found = std::find_if( vec.begin(), vec.end(),   
                pred ) != vec.end();   
      
   the intent would be even clearer. Personally I find this idiom common   
   enough that I think it should be its own algorithm:   
      
        bool found = contains_if( vec.begin(), vec.end(), pred );   
      
   By declaring the variable before the loop, and breaking out in the   
   middle, you are effectively creating a puzzle that readers must decode.   
   When you use an algorithm, you are telling them the answer. It's right   
   there in the name: find_if, or contains_if.   
      
   -- Dave Harris, Nottingham, UK.   
      
      
   --   
         [ 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