【发布时间】:2012-10-19 07:16:59
【问题描述】:
const auto& 如果我想执行只读操作就足够了。但是,我遇到了
for (auto&& e : v) // v is non-const
最近几次。这让我想知道:
与auto& 或const auto& 相比,使用转发引用是否有可能在某些不明显的极端情况下获得一些性能优势?
(shared_ptr 是不知名的角落案例的嫌疑人)
更新 我在收藏夹中找到的两个示例:
Any disadvantage of using const reference when iterating over basic types?
Can I easily iterate over the values of a map using a range-based for loop?
请专注于这个问题:我为什么要在基于范围的 for 循环中使用 auto&&?
【问题讨论】:
-
你真的“经常”看到它吗?
-
我不确定你的问题中是否有足够的背景信息来衡量你看到它的地方有多“疯狂”。
-
@LightnessRacesinOrbit 长话短说:我为什么要在基于范围的 for 循环中使用
auto&&?
标签: c++ performance for-loop c++11 move-semantics