for(auto& entity : memoryManager.getItems()) entity->update(mFrameTime);
如果 memoryManager 包含 1000 个项目,在循环开始时 memoryManager.getItems() 会被调用 1000 次还是只调用一次?
If memoryManager contains 1000 items, does memoryManager.getItems() get called 1000 times or only one at the beginning of the loop?
编译器是否使用 -O2(或 -O3)运行任何优化?
Does the compiler run any optimization with -O2 (or -O3)?
(memoryManager.getItems() 返回一个 std::vector)
它只评估一次.该标准将基于范围的 for 语句定义为等效于:
It is only evaluated once. The standard defines a range-based for statement as equivalent to:
{
auto && __range = range-init;
for ( auto __begin = begin-expr, __end = end-expr; __begin != __end; ++__begin ) {
for-range-declaration = *__begin;
statement
}
}
其中 range-init 是 之后的表达式(用括号括起来)或花括号初始化列表:
这篇关于是否每个循环都评估基于 C++11 范围的 for 循环条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
编译器如何处理编译时分支?What do compilers do with compile-time branching?(编译器如何处理编译时分支?)
我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗?)
在 C/C++ 中检查空指针Checking for NULL pointer in C/C++(在 C/C++ 中检查空指针)
比较运算符的数学式链接-如“if((5<j<=1))&quMath-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比较运算符的数学式链接-如“if((5<j<=1)))
“if constexpr()"之间的区别与“if()"Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之间的区别与“if())
C++,'if' 表达式中的变量声明C++, variable declaration in #39;if#39; expression(C++,if 表达式中的变量声明)