转发转发参考的单个成员 [英] Forward individual members of a Forward reference
问题描述
我有一个函数可以通过其成员移动通用参数.这些选项中哪个更正确:
I have a function that potentially moves a generic argument but through their members. What of these options is more correct:
-
这似乎更自然,但很奇怪,因为该参数可能会移动两次[a],这很奇怪,因为对象可能变得无效.
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]正如@Angew所指出的那样,该语句是不正确的,它仅看起来 好像被移动了两次. std::forward
(如std::move
)实际上不移动任何内容.最多移动成员(通过后续操作decltype(myhead)::operator=
,但这恰恰是目标.)
[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.)
推荐答案
您的第一个代码非常好:
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_;
}
这是因为该标准保证当执行a.b
和a
时是x值(例如转发的rvalue引用),a.b
的结果也是一个exvalue(即可以从中移出).另请注意,std::forward
和std::move
本身并不进行任何实际的 moving 动作,它们只是强制转换.因此,在代码中两次从t
移出没有任何风险.
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.
这篇关于转发转发参考的单个成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!