In C++11 we can now do :
void dosomething( std::vector<Thing>& things )
{
for( Thing& thing : things )
{
dofoo( thing );
wiizzz( thing );
tadaa( thing );
}
}
I know that the addition and use of lambda is syntactic sugar but it provide interesting optimization opportunities.
What about the for loop? Is it only syntactic sugar or can the compiler optimize some cases that it couldn't or would be t开发者_如何学运维oo hard to do with handwritten loop?
It's just a syntactic sugar since the standard says that it's equivalent to a loop with iterators [ Edit: this means it doesn't provide any additional information to the compiler compared to the equivalent for loop — end edit ]. You may get a better performance though since it's equivalent to:
for(auto iter = con.begin(), end = con.end(); iter != end; ++iter)
{
auto& ref = *iter;
// ...
}
while most people may write:
for(auto iter = con.begin(); iter != con.end(); iter++)
{
// use *iter directly
// ...
}
which may be slower if the con.end(), iter++ or *iter are not trivial.
[ Edit:
lambda is syntactic sugar
Not really. Unlike for loop it allows the compiler to capture the stack-frame base pointer directly, for variables captured by reference this saves one address indirection per each use, compared to a handcrafted function object. — end edit ]
Maybe, but probably not. It does eliminate the possible creating of an index/counter variable that won't be used. That's not required for a normal for loop either, but it's a lot more likely to happen just because it's what some people are accustomed to doing.
Realistically, it's unlikely to make any difference even from that though. I, at least, find it hard to imagine a compiler team so sophisticated that they have even the slightest aspiration toward supporting C++0x, that hasn't already handled the relatively trivial point of detecting and eliminating creating and incrementing a loop index that's never used.
精彩评论