Thanks for the answer.
I'm very sorry but i'm still a bit confused.
You said that
In static_cast<obj&&>(ref) OR o1 = ref
ref has type 'obj' and value category 'lvalue'
|
Yes. Ref, since it has a name and an address, is indeed an lvalue.
When people talk about std::move() they say: move returns a temporary... but that's not true as far as I know!
I looked at move's implementation, and that's just a static_cast which casts whatever the original type is, into an rvalue reference and returns it.
So if I'm calling move() with an lvalue
int a = 10; move(a)
move() is supposed to return an rvalue reference to
a.
It's wrong to say that
a magically becomes a temporary.
If what I've said is correct, then I at least understood how move() works.
With that said...
o1 = ref
The code doesn't compile because the Move Assignment is
expecting a temporary that the rvalue reference can bind to.
Ref is not a temporary, but a lvalue. So alright.
o1 = static_cast<obj&&>(ref);
This compiles...
and doesn't make sense to me.
o1 = move(ref);
We said earlier that move() takes any type and makes it an rvalue reference.
This, to me, is as confusing as the last example.
We're casting an rvalue reference to an rvalue reference.
This time, since move() is a function, we also return it.
---
Let's put it this way:
The Move Assignment wants a temporary.
o1 = ref
can't give it because ref is not a temporary
o1 = static_cast<obj&&>(ref)
why does it return a temporary?
o1 = move(ref)
why does it return a temporary? move() is supposed to return a REFERENCE (obj&&) and NOT A TEMPORARY
In other words, even if the type of a variable is 'rvalue reference to X', the id-expression consisting of the name of the variable yields an lvalue of (non-reference) type X.
|
Has this rule an official documentation page?