我有3个问题:
我可以将左值直接绑定到右值引用吗?
那个存在的对象会发生什么
std::move()
?std::move 和 有什么区别
std::forward
?
struct myStr{
int m_i;
};
void foo(myStr&& rs) { }
myStr rValueGene()
{
return myStr();
}
int main()
{
myStr mS= {1};
foo(rValueGene()); //ok passing in modifiable rvalue to rvalue reference
// To Question 1:
//below initilize rvalue reference with modifiable lvalue, should be ok
//but VS2010 gives a compile error: error C2440: 'initializing' : cannot convert from 'myStr' to 'myStr &&'
//Is this correct ?
myStr&& rvalueRef = mS;
//by using std::move it seems ok, is this the standard way of doing this
//to pass a lvalue to rvalue reference
//myStr&& rvalueRef = std::move(mS);
// To Question 2:
//also what happens to mS object after std::move ?
//destroyed , undefined ?
}