std::move 的类型是什么?
此代码按预期工作(在线此处)。 最后,v
为空,w
也不为空,因为它窃取了 v
的内容。
vector<int> v;
v.push_back(1);
cout << "v.size(): " << v.size() << endl;
auto vp = move(v);
vector<int> w(vp);
cout << "w.size(): " << w.size() << endl;
cout << "v.size(): " << v.size() << endl;
但是,如果我将 auto vp=move(v)
替换为
vector<int> && vp = move (v);
那么它就不会移动。相反,它会复制并且两个向量最后都非空。如此处所示。
澄清:更具体地说,vp
的自动派生类型是什么?如果它不是vector
,那还能是什么呢?尽管这两个示例如此相似,但为什么会给出不同的结果?
额外:我也尝试过,它仍然是复制而不是移动
std :: remove_reference< vector<int> > :: type && vp = move(v);
This code works as expected (online here).
At the end v
is empty and w
is not empty as it has pilfered the contents of v
.
vector<int> v;
v.push_back(1);
cout << "v.size(): " << v.size() << endl;
auto vp = move(v);
vector<int> w(vp);
cout << "w.size(): " << w.size() << endl;
cout << "v.size(): " << v.size() << endl;
But if I replace auto vp=move(v)
with
vector<int> && vp = move (v);
Then it doesn't move. Instead it copies and both vectors are non-empty at the end. As shown here.
Clarification: More specifically, what is the auto-derived type of vp
? If it's not vector<int> &&
, then what else could it be? Why do the two examples give different results despite being so similar?
Extra: I also tried this, and it still copied instead of moving
std :: remove_reference< vector<int> > :: type && vp = move(v);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑OP的澄清:
move(v)
的auto
派生类型是vector
。请参阅 C++11“自动”语义。第一个示例执行此操作:
第二个示例执行此操作:
std:move
所做的只是将类型转换为右值(请参阅 什么是 std::move(),何时应该使用它?)。因此,它只是将右值引用
vp
设置为v
而没有执行任何其他操作。此外,右值引用是左值(它有一个名称),因此将调用复制构造函数将
vp
(即v
)复制到w< /代码>。
您将
vp
设置为右值,它将调用移动构造函数(示例):如果 可能想读一下:C++ Rvalue References解释了。
Edit for OP's clarification: the
auto
-derived type ofmove(v)
isvector<int>
. See C++11 "auto" semantics.The 1st example does this:
and the 2nd example does this:
What
std:move
does is simply casting a type to an rvalue (See What is std::move(), and when should it be used?). Therefore, init's just setting the rvalue-reference
vp
tov
and do nothing else. Also, an rvalue-reference is an lvalue (it has a name), sowill call the copy constructor to copy
vp
(which isv
) intow
.It will call the move constructor if you make
vp
an rvalue (Example):You may want to read this: C++ Rvalue References Explained.
在第一种情况下:
相当于:
这会调用移动构造函数,因为
move(v)
的类型为vector&&
,因此vp
最终窃取了v
的内容。在第二种情况下:
仅使
vp
成为对v
的右值引用。这不会导致移动构造函数或复制构造函数被调用,并且不会窃取任何内容。In the first case:
is equivalent to:
This invokes the move constructor, since
move(v)
has typevector<int>&&
, hencevp
ends up pilfering the contents ofv
.In the second case:
just makes
vp
an r-value reference tov
. This doesn't result in the move constructor or copy constructor being called, and nothing is pilfered.正在创建一个新的
vector
并调用它的移动构造函数:这将窃取
v
的内容。因此,“vp”的类型只是
vector
.. 不涉及任何引用:它是“移动”内部结构 .. 而不是实际的向量本身。
因此
&vp
将与&v
不同。但内容会移动。Is creating a new
vector<int>
and calling it's move constructor:Which will steal the contents of
v
.So type of 'vp' is simply
vector<int>
.. no references involved:It's 'moving' the internals .. not the actual vector itself.
So
&vp
will be different to&v
.. but the contents will move across.