Skip to main content
Commonmark migration
Source Link
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.

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.

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.

deleted 328 characters in body
Source Link
eerorika
  • 239.8k
  • 13
  • 212
  • 354
void test(const std::string& a) std::string x{"test"}; test(std::move(x)) 

Why does this version print && and I don't get an error?

Because const lvalue references bind to rvalues. When they do, a temporary object is created, whose lifetime is bound to the lifetime of that lvalue reference.

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.

void test(const std::string& a) std::string x{"test"}; test(std::move(x)) 

Why does this version print && and I don't get an error?

Because const lvalue references bind to rvalues. When they do, a temporary object is created, whose lifetime is bound to the lifetime of that lvalue reference.

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.

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.

added 43 characters in body
Source Link
eerorika
  • 239.8k
  • 13
  • 212
  • 354
void test(const std::string& a) std::string x{"test"}; test(std::move(x)) 

Why does this version print && and I don't get an error?

Because const lvalue references bind to rvalues. When they do, a temporary object is created, whose lifetime is bound to the lifetime of that lvalue reference.

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. Names of variables are lvalues. Therefore, 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.

void test(const std::string& a) std::string x{"test"}; test(std::move(x)) 

Why does this version print && and I don't get an error?

Because const lvalue references bind to rvalues. When they do, a temporary object is created, whose lifetime is bound to the lifetime of that lvalue reference.

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

a is an lvalue expression. Names of variables are lvalues. 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.

void test(const std::string& a) std::string x{"test"}; test(std::move(x)) 

Why does this version print && and I don't get an error?

Because const lvalue references bind to rvalues. When they do, a temporary object is created, whose lifetime is bound to the lifetime of that lvalue reference.

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.

Source Link
eerorika
  • 239.8k
  • 13
  • 212
  • 354
Loading