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 33,113 of 33,346   
   Dave Harris to Gon Solo   
   Re: Fixed for loop   
   29 Jun 13 05:51:49   
   
   From: brangdonj@googlemail.com   
      
   In article <8a7337d8-16f3-493d-8b64-c0101b79b755@googlegroups.com>,   
   gonsolo@googlemail.com (Gon Solo) wrote:   
   > Is there a better way in C++ to make a very simple for loop look   
   > very simple?   
      
   It depends on what you mean by "better". I don't think your macro adds   
   enough value to offset the drawbacks of being a macro. It could be   
   written as a template:   
      
   template    
   void n_times( N n, const Fn &fn ) {   
        for (N i = N(); i < n; ++i)   
            fn( i );   
   }   
      
   void demo() {   
        n_times( 3, []( int i ) {   
            use( i );   
        } );   
   }   
      
   I'd say that does a better job of encapsulating the logic - for   
   example, by preventing the loop body from modifying the loop counter -   
   but the usage syntax isn't especially simple.   
      
   -- 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