c++ranged-loops

Read-only ranged based for


Let us consider the following example (of course this should be replaced by std::accumulate):

#include <vector>

auto sum(std::vector<int> const& numbers) -> int
{
    auto sum = 0;
    for(auto /*const, const&, &&, [nothing]*/ i : numbers) { sum += i; }
    return sum;
}

As you see, there are many different ways to use the ranged-based for loop for small types. Note that all of these variants compiled to the same assembly code in compiler explorer using gcc.

I often see the recommendation to use auto i in the first case and auto const& i in the second case.

But we are talking to the compiler and not the human here. The interesting information is that the variable is only input. This is not expressed by auto i.

So is there any performance-disadvantage of using auto const& i instead of auto i in any situation where you only need to read the input?


Solution

  • Do not overthink this. In C and C++ there is "AS IF RULE" which causes that any version of your example doesn't lead to any changes in resulting machine code.

    See this godbolt: https://godbolt.org/z/3rnWrr Explenation: note each compiler command line arguments is defining VERSION macro, to provide auto, auto& or auto const &.

    So basically if vector contains simple type (like build ins) just use auto since it is more convenient. In other case with complex types, you need to think about it a bit more and decide between auto and auto&. If have doubts do some measurements or check assembly.