r/learncpp • u/6_oz • Dec 11 '19
What does it mean to '...cast an lvalue to its corresponding rvalue reference type.'
Reading C++ Primer 5th edition, and I came across this passage when it was introducing std::move,
'Although we cannot directly bind an rvalue reference to an lvalue, we can explicitly cast an lvalue to its corresponding rvalue reference type.'
Could someone clarify what this means? I read over the lvalue and rvalue reference section, searched on the internet etc
I would appreciate a concrete example.
4
Upvotes
4
u/jedwardsol Dec 11 '19
https://godbolt.org/z/p3ii-c
2
u/elperroborrachotoo Dec 11 '19 edited Dec 11 '19
Functionally, it is indeed just a cast, similar to casting from
T&toT const &.We do that because it affects parameter matching and thus overload resolution: the value now is "accepted" by functions that expect an rvalue reference. See:
Does that help?
[edith] "Although we cannot directly bind an rvalue reference to an lvalue" means that
Is an error, because
myvis an lvalue, but `Bar expects an rvalue reference, and this conversion doesn't happen automatically (unlike the conversion from an lvalue to an lvalue reference, or const-reference).