&& Rvalue ref

closed account (SECMoG1T)
Hi , av got another question here about Rvalues and template parameters, should Rvalue references bound to template parameters allow modification of the arguments supplied at function calls????
I can't understand why Rvalues should be modifiable anyway.

Note: the functions below compile with no warnings or errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 template<typename X,typename Y>
void Rvalue_mod(X&& a, Y&& b)
{
    ++a; --b;
  std::cout<<"in mod : "<<a<<"  --  "<<b<<std::endl;
}

template<typename T,typename N>
void with_Rvalue(T&& n,N&& b)
{
    std::cout<<"Before : "<<n<<"  --  "<<b<<std::endl;
    Rvalue_mod(n,b);
    std::cout<<"After  : "<<n<<"  --  "<<b<<std::endl;
}

int main()
{
    with_Rvalue(23,23);
}


Thanks in advance.
Within (the block scope of) the function
template<typename X,typename Y> void Rvalue_mod(X&& a, Y&& b)
the expressions (a) and (b) are lvalues.

When we do: int&& r = 23 ; a temporary object of type int initialised with 23 is created and the rvalue reference is bound to that object.

When we do: const int&& r = 23 ; a temporary object of type const int initialised with 23 is created and the rvalue reference to const is bound to that object

When we do: int i = 23 ; int&& r = static_cast<int&&>(i) ; no temporary object is created; the rvalue reference is bound directly to i
closed account (SECMoG1T)
Well thank you very much @JLBorges but one more question , what does this mean
Within (the block scope of) the function the expressions (a) and (b) are lvalues.??

Does it mean
temporary object created 
are lvalues ?? or how are they lvalues because all the values in the scope are still bound to && ? maybe i missed something.

Thank you.

Is an Rvalue Reference an Rvalue?
...
Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue.

In the example above, the thing that is declared as an rvalue reference has a name, and therefore, it is an lvalue:
1
2
3
4
void foo(X&& x)
{
  X anotherX = x; // calls X(X const & rhs)
}

Here is an example of something that is declared as an rvalue reference and does not have a name, and is therefore an rvalue:
1
2
3
X&& goo();
X x = goo(); // calls X(X&& rhs) because the thing on
             // the right hand side has no name 

And here's the rationale behind the design: Allowing move sematics to be applied tacitly to something that has a name, as in
1
2
  X anotherX = x;
  // x is still in scope! 

would be dangerously confusing and error-prone because the thing from which we just moved, that is, the thing that we just pilfered, is still accessible on subsequent lines of code. But the whole point of move semantics was to apply it only where it "doesn't matter," in the sense that the thing from which we move dies and goes away right after the moving. Hence the rule, "If it has a name, then it's an lvalue."
http://thbecker.net/articles/rvalue_references/section_05.html

Read the whole article, starting from page one, (carefully).
closed account (SECMoG1T)
Wow that explains it ,now i get it, Thank very much you sir for that.
Topic archived. No new replies allowed.