PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 10, 2022

[FIXED] why do ranges algorithms take rvalue reference as argument

 July 10, 2022     c++, c++20, reference, std-ranges     No comments   

Issue

if I take for example the ranges::fill algorithm:

https://en.cppreference.com/w/cpp/algorithm/ranges/fill

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 a rvalue reference as argument ( R&& r) ? I would have expected it to take a 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)
    


Answered By - dfrib
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing