Vector push_back move implementation

China☆狼群 提交于 2021-02-10 03:06:33

问题


In my textbook, the implementation of the vector push_back move implementation is:

void push_back( Object && x )
{
    if( theSize == theCapacity )
        reserve( 2 * theCapacity + 1 );
    objects[ theSize++ ] = std::move( x );
}

My understanding of std::move is that it basically static casts the item as an rvalue reference. So why on the last line did they have to use std::move( x ), when x was passed in already as an rvalue reference?


回答1:


x is an rvalue reference, but the rule of thumb you must follow is: if it has a name, it's an lvalue. Therefore you must apply std::move to convert its type to an rvalue. If you left out the std::move then x would be copied instead of moved into its destination. More information can be found in Rvalue References Explained.



来源:https://stackoverflow.com/questions/33205686/vector-push-back-move-implementation

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