c++ - Are return types considered as rvalues? -
just simple question, trying simulate bad practice when comes returning &&
. aware it's bad practice.
here example
//dummy empty class dummy && crashtest(){ dummy temp; return std::move(temp); } int main() { dummy && k = crashtest(); return 0; }
this bad practice because k
holds reference destructed temp
inside function.
i tried different approach doing inside main
:
dummy l; //generate temp dummy && k = std::move(l); //return value dummy && d = k; // copy
though l
won't destroyed, how come won't let me compile? error saying trying bind lvalue. isn't same happens in function? understanding, happened in function was:
temp created -> std::move(temp) -> return type dummy&&
receives -> k
equalized return type temporary.
i need clarification. return type considered rvalue
?
bonus question: happens if rvalue reference variable equalized rvalue reference variable? &&lhs = &&rhs
, lhs takes rhs reference?
returning rvalue reference function cause dangling reference(the reference exists, temporary object refers has been destroyed). called xvalue(an "expiring" value) in standard.
Comments
Post a Comment