【发布时间】:2020-05-13 08:14:54
【问题描述】:
我在实现list 类的代码中发现了以下sn-p
void push_front( const T & x ) { insert( begin( ), x ); }
void push_front( T && x ) { insert( begin( ), std::move( x ) );}
现在我知道,如果我有一个函数将参数作为r-value,那么该参数将是函数范围内的l-value(不是吗?)。
所以我可以用
替换之前的sn-pvoid push_front( const T & x ) { insert( begin( ), x ); }
void push_front( T && x ) { push_front( x );}
第一个问题:我说的对吗?
第二个:考虑到第一个sn-p中的r-value参数是第二个函数中的l-value参数,将std::move( x )将x从l-value转换为r-value和函数push_front() 调用r-value 版本的函数insert() 还是什么?
编辑::
insert() 就是这样实现的
iterator insert( iterator itr, const T & x )
{
Node *p = itr.current;
theSize++;
return { p->prev = p->prev->next = new Node{ x, p->prev, p } };
}
iterator insert( iterator itr, T && x )
{
Node *p = itr.current;
theSize++;
return { p->prev = p->prev->next = new Node{ std::move( x ), p->prev, p } };
}
Node的定义
struct Node
{
private:
T data;
Node *prev;
Node *next;
Node( const T & d = T{ }, Node * p = nullptr,
Node * n = nullptr )//It's possible because of const
:data{ d }, prev{ p }, next{ n } { }
Node( T && d, Node * p = nullptr, Node * n = nullptr )
: data{ std::move( d ) }, prev{ p }, next{ n } { }
};
【问题讨论】:
-
您建议的替换将复制参数,而不是像第一个示例那样移动它。你做出改变是否正确取决于这是否是你想要的行为......
-
几乎所有你想知道的都可以在这里找到:stackoverflow.com/questions/3106110/what-is-move-semantics
-
@M.M 我的意思是如果
std::move(x)将x 转换为rvalue,那么将调用push_front(T&& )而不是push_front(const T& ) -
@MM 如果我只有两个版本的
push_front()(即)push_front(T&& )和push_front(const T& ),因此我的对象不会被复制,我的替换会更好,不是吗? -
@anonymous 您的替换将复制对象而不是移动它。代码
push_front( std::move(x) )移动对象而push_front(x)不移动
标签: c++ move-semantics lis