Vik*_*ehr 3 c++ foreach c++11 visual-studio-2012
写作时
for(const auto& val: my_container)
sum += val
Run Code Online (Sandbox Code Playgroud)
Visual Studio选择了可变版本begin(),这是设计还是错误?
当我在写容器上使用副本时,这在我的代码中是一个非常严重的性能问题.
这是设计的.在确定是否将其my_container视为const 时,"foreach"循环不会查看迭代变量的限定符或引用限定符.解决方法是显式添加const
const auto& my_container_const = my_container;
for(const auto& val: my_container_const)
sum += val
Run Code Online (Sandbox Code Playgroud)