From
http://en.cppreference.com/w/cpp/language/static_cast
If new_type is an rvalue reference type, static_cast converts the value of glvalue, class prvalue, or array prvalue any lvalue expression to xvalue referring to the same object as the expression |
That underlined sentence!
I created a struct with a constructor and a copy constructor.
1 2 3 4 5
|
Obj o1; // calls constructor
Obj o2 = static_cast<Obj&&>(o1); // calls copy constructor
static_cast<Obj&&>(o1); // calls nothing
|
The temporary object that static_cast returns has the same properties of the object
o1.
This temporary
must have been created in some way!
Look:
The code above calls BOTH the
constructor for the temporary AND the
copy constructor for the lvalue
So, in this example, before constructing
a the temporary was constructed first.
---
In the first code, instead, no temporary has actually been
created because the constructor for it isn't called.
I'm fully aware that, if a Move Assignment Operator had been overloaded, it would've been called and the resources held by
o1 could have been moved to
o2 leaving o1 in a "moved-from", empty state.
What I'm actually trying to understand is
what happens to o1 when static_cast<Obj&&>(o1)
is invoked on him?
A trivial answer would be: it is just casted to a temporary
But what does it mean!?!?
Another trivial answer would be: it just returns an rvalue reference that refers to o1
But (1) rvalue reference cannot bind to lvalue and (2) if that was true I couldn't do
|
Obj o2 = static_cast<Obj&&>(o1);
|
what happens to o1 when static_cast<Obj&&>(o1)
is invoked on him?