Forward individual members of a Forward reference

萝らか妹 提交于 2020-01-03 18:52:31

问题


I have a function that potentially moves a generic argument but through their members. What of these options is more correct:

  1. 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_;
    }
    
  2. 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_);
    }
    
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!