Boost logo

Boost :

From: Stefan Strasser (sstrasser_at_[hidden])
Date: 2005-05-02 14:52:33


Eric Niebler schrieb:
>
> Stefan Strasser wrote:
>
>> Dave Harris schrieb:
>>
>>> For example, from looking at the implementation it seems that the
>>> macro caches the end() value in a variable called _foreach_end.
>>
>>
>> it is cached, but why?
>> end is end and stays end, unless it is invalidated.
>>
>
> Performance. Even when writing loops by hand, it's a good idea to save
> the result of .end() in a local to avoid repeated function calls that
> are not necessary. It's called "hoisting". It's one of the side-benefits
> of using FOREACH and std:: algorithms.
>
>

I think it's not worth it here.
besides that it isn't a performance overhead on most containers you
don't expect that from a keyword.

and it is not consistent:

   char str[]={'a','b',0,0};
   foreach(char c,str){
     str[2]='c';
     std::cerr << c << std::endl;
   }

output is "abc".

   std::string str="ab";
   foreach(char c,str){
     if(str.length() == 2) str+='c';
     std::cerr << c << std::endl;
   }

output is "ab".

-- 
Stefan Strasser

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk