问题
I have a function that potentially moves a generic argument but through their members. What of these options is more correct:
This seems the more natural but it is strange because the argument is potentially moved twice [a], which is odd because the object can become invalid.
template<class T> void fun(T&& t){ myhead_ = std::forward<T>(t).head_; myrest_ = std::forward<T>(t).rest_; }
This can't be incorrect but it may not be moving anything.
template<class T> void fun(T&& t){ myhead_ = std::forward<decltype(t.head_)>(t.head_); myrest_ = std::forward<decltype(t.rest_)>(t.rest_); }
This seems correct but too much code.
template<class T> void fun(T& t){ myhead_ = t.head_; myrest_ = t.rest_; } template<class T> void fun(T&& t){ myhead_ = std::move(t.head_); myrest_ = std::move(t.rest_); }
[a] This statement is incorrect as @Angew pointed out, it only looks as if it is moved twice. std::forward
(like std::move
) doesn't actually move anything. At most the member is moved (by the subsequent operation decltype(myhead)::operator=
but that is precisely the objective.)
回答1:
Your first code is perfectly fine:
template<class T>
void fun(T&& t){
myhead_ = std::forward<T>(t).head_;
myrest_ = std::forward<T>(t).rest_;
}
That is because the standard guarantees that when doing a.b
and a
is an xvalue (such as a forwarded rvalue reference), the result of a.b
is also an exvalue (i.e. can be moved from). Also note that std::forward
and std::move
do not do any actual moving themselves, they're just casts. So there is no risk in moving from t
twice in your code.
来源:https://stackoverflow.com/questions/50149972/forward-individual-members-of-a-forward-reference