17

This may be obvious but I think it is something difficult to me. Given this:

void test(std::string&&) { } std::string x{"test"}; test(std::move(x)); // ok 

This code calls test() with a rvalue reference as parameter so the program compiles as I expect.

Now look at this:

void other_test(const std::string&) { } std::string x{"test"}; other_test(std::move(x)); // ok??? 

And here I'm tilted. Why does this version compile? The std::move returns a && type; why then I don't get an error in the second method where I use const&?


I know that

int&& s = 5; const int& s = 5; 

is valid because in both cases I provide something that has not an lvalue, it has no addresses. Are && and const& equivalent? If no, are there differences?

7
  • I cannot understand why const& and && behave in the same way. What is the difference? I expected that the const& version would fail and not print the text Commented Aug 30, 2018 at 20:10
  • I expect an error! From my understanding a move should return an && type and so only an && parameter should work. Why then a const& parameter works? Commented Aug 30, 2018 at 20:12
  • 7
    Is the question "Why can a const& bind to an rvalue when I expect it to fail?" Commented Aug 30, 2018 at 20:14
  • 1
    @Barry yes that is the question Commented Aug 30, 2018 at 20:15
  • 2
    I think some clarification is needed. Are void test(std::string&& a) and void test(const std::string& a) both in scope when you do your tests? Commented Aug 30, 2018 at 20:15

5 Answers 5

19

std::move doesn't actually move anything out of it's own. It's just a fancy name for a cast to a T&&. Calling test like this test(std::move(x)); only shows that a T&& is implicitly convertible to a const T&. The compiler sees that test only accepts const T& so it converts the T&& returned from std::move to a const T&, that's all there is to it.

Sign up to request clarification or add additional context in comments.

14 Comments

Ok thank you! Now I'd have another question, Why should I declare the paremeter as && when I have const&?
@EmmaRossignoli Because you can't modify an object through a const reference. The point of of using rvalue references and move semantics is to modify the object by stealing its resources.
@EmmaRossignoli If you declare it as T&& then you're saying "test should only accept an rvalue-reference". It doesn't say anything or impose any requirements on the caller when it comes to how the caller got that rvalue-ref.
@EmmaRossignoli Read this answer stackoverflow.com/questions/3106110/what-are-move-semantics/… and understand it it is all explained there
Now I understand that && can be converted to const& but the difference is that a const& cannot be edited (of course due to the const) while the && yes. My doubt is solved thank you all
|
18

In simple terms:

  • && can bind to non-const rvalues (prvalues and xvalues)
  • const && can bind to rvalues (const and non-const)
  • & can bind to non-const lvalues
  • const & can bind to rvalues (prvalues and xvalues) and lvalues (const and non-const for each). A.k.a. to anything.

Comments

5

If you want a function to expressly allow const-Lvalue objects, but expressly disallow Rvalue objects, write the function signature like this:

void test(const std::string&) { } void test(std::string&&) = delete;//Will now be considered when matching signatures int main() { std::string string = "test"; test(string);//OK //test(std::move(string));//Compile Error! //test("Test2");//Compile Error! } 

Comments

2
test(std::string&& a) { something(a) //--> not moved because it has lvalue 

Names of variables are lvalues. a is a name of a variable, therefore a is an lvalue expression, and therefore it will not be moved from.

It's unclear what you mean by "has". a is an expression. It is a name of a reference, and references refer to objects. Value categories pertain to expressions, not objects.

test(const std::string& a): a is const lvalue reference and like before I have lvalue and rvalue. And plus more, in this case if I called

std::move(a) 

where a is a const& the move works!

If by "works" you mean that it invokes a move constructor or assignment, then no, it does not work because no move construction or assignment has happened.

2 Comments

There is no temporary object. std::move(x) is an xvalue, thus a glvalue.
@xskxzr I removed that part of the answer.
2

When you call std::move(x), an rvalue reference to the underlying data, test, will be returned. You are allowed to pass rvalue references as const (and const only!) reference parameters because an rvalue reference is implicitly convertible to a const reference. They are arguably the same thing from the function's point of view (a read only parameter). If you removed the const-qualifier of your parameter, this code would not compile:

void other_test(std::string&) { } std::string x{"test"}; other_test(std::move(x)); //not okay because //the function can potentially modify the parameter. 

See Bo Qian's youtube video on rvalue vs lvalue.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.