Search code examples
c++referencec++20std-ranges

Why do ranges algorithms take rvalue reference as argument


If I take for example the ranges::fill algorithm, the signature is:

template< class T, ranges::output_range<const T&> R >
constexpr ranges::borrowed_iterator_t<R> fill( R&& r, const T& value );

And an example use:

#include <algorithm>
#include <vector>
#include <iostream>
 
int main()
{
    std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    namespace ranges = std::ranges;
    ranges::fill(v, 10);
}

Why does ranges::fill take an rvalue reference as argument (R&& r)? I would have expected it to take an lvalue reference (R& r) instead.


Solution

  • Since R is a template parameter, R&& is not an rvalue reference, it is a forwarding/universal reference.

    Forwarding references

    Forwarding references are a special kind of references that preserve the value category of a function argument, making it possible to forward it by means of std::forward. Forwarding references are either:

    1. function parameter of a function template declared as rvalue reference to cv-unqualified type template parameter of that same function template:

      template<class T>
      int f(T&& x) {                    // x is a forwarding reference
          return g(std::forward<T>(x)); // and so can be forwarded
      }
      
      int main() {
          int i;
          f(i); // argument is lvalue, calls f<int&>(int&), std::forward<int&>(x) is lvalue
          f(0); // argument is rvalue, calls f<int>(int&&), std::forward<int>(x) is rvalue
      }
      
      template<class T>
      int g(const T&& x); // x is not a forwarding reference: const T is not cv-unqualified
      
      template<class T> struct A {
          template<class U>
          A(T&& x, U&& y, int* p); // x is not a forwarding reference: T is not a
                                   // type template parameter of the constructor,
                                   // but y is a forwarding reference
      };
      
    2. auto&& except when deduced from a brace-enclosed initializer list:

      auto&& vec = foo();       // foo() may be lvalue or rvalue, vec is a forwarding reference
      auto i = std::begin(vec); // works either way
      (*i)++;                   // works either way
      g(std::forward<decltype(vec)>(vec)); // forwards, preserving value category
      
      for (auto&& x: f()) {
        // x is a forwarding reference; this is the safest way to use range for loops
      }
      
      auto&& z = {1, 2, 3}; // *not* a forwarding reference (special case for initializer lists)