Is a C ++ 11 cycle defined for each cycle?

for(auto& entity : memoryManager.getItems()) entity->update(mFrameTime); 

If memoryManager contains 1000 elements, does memoryManager.getItems() call 1000 times or only one at the start of the loop?

Does the compiler perform optimizations with -O2 (or -O3)?

( memoryManager.getItems() returns a std::vector<Entity*>& )

+47
c ++ performance foreach for-loop c ++ 11
Apr 02 '13 at
source share
1 answer

It is evaluated only once. The standard defines an operator based on the for range as the equivalent:

 { auto && __range = range-init; for ( auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ) { for-range-declaration = *__begin; statement } } 

where range-init is an expression (surrounded by parentheses) or the bit-init-list after :

+46
Apr 02 '13 at
source share



All Articles