Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I assign something to rValue string?

Tags:

std

c++11

As I expected this code doesn't compile because move gives you rValue and you can't assign anything to rValue:

int a, b;
move(a) = b;

The question is why below code compiles?

string a, b;
move(a) = b;
like image 399
user1723095 Avatar asked Jan 31 '26 11:01

user1723095


1 Answers

The reason is a bit surpring, probably. The first form is the built-in assignment, and that indeed does not work on rvalue references.

The second statement is actually move(a).operator=(b). That is a function call. You can call functions on rvalues, even if the functions have somewhat unusual names.

like image 141
MSalters Avatar answered Feb 03 '26 11:02

MSalters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!