std::move 的类型是什么?

发布于 2024-12-22 05:22:53 字数 983 浏览 2 评论 0原文

此代码按预期工作(在线此处)。 最后,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说好的呢 2024-12-29 05:22:53

编辑OP的澄清move(v)auto派生类型是vector。请参阅 C++11“自动”语义

第一个示例执行此操作:

move 'v' into 'vp'
copy 'vp' into 'w'

第二个示例执行此操作:

set 'vp' as rvalue-reference of 'v'
copy 'vp' (which is 'v') into 'w'

std:move 所做的只是将类型转换为右值(请参阅 什么是 std::move(),何时应该使用它?)。因此,

vector<int>&& vp = move(v);

它只是将右值引用 vp 设置为 v 而没有执行任何其他操作。此外,右值引用是左值(它有一个名称),因此

vector<int> w(vp);

将调用复制构造函数将 vp (即 v)复制到 w< /代码>。

您将 vp 设置为右值,它将调用移动构造函数(示例):

vector<int> w(move(vp))

如果 可能想读一下:C++ Rvalue References解释了

Edit for OP's clarification: the auto-derived type of move(v) is vector<int>. See C++11 "auto" semantics.

The 1st example does this:

move 'v' into 'vp'
copy 'vp' into 'w'

and the 2nd example does this:

set 'vp' as rvalue-reference of 'v'
copy 'vp' (which is 'v') into 'w'

What std:move does is simply casting a type to an rvalue (See What is std::move(), and when should it be used?). Therefore, in

vector<int>&& vp = move(v);

it's just setting the rvalue-reference vp to v and do nothing else. Also, an rvalue-reference is an lvalue (it has a name), so

vector<int> w(vp);

will call the copy constructor to copy vp (which is v) into w.

It will call the move constructor if you make vp an rvalue (Example):

vector<int> w(move(vp))

You may want to read this: C++ Rvalue References Explained.

茶花眉 2024-12-29 05:22:53

在第一种情况下:

auto vp = move(v);

相当于:

vector<int> vp = move(v);

这会调用移动构造函数,因为 move(v) 的类型为 vector&&,因此 vp 最终窃取了 v 的内容。

在第二种情况下:

vector<int>&& vp = move(v);

仅使 vp 成为对 v 的右值引用。这不会导致移动构造函数或复制构造函数被调用,并且不会窃取任何内容。

In the first case:

auto vp = move(v);

is equivalent to:

vector<int> vp = move(v);

This invokes the move constructor, since move(v) has type vector<int>&&, hence vp ends up pilfering the contents of v.

In the second case:

vector<int>&& vp = move(v);

just makes vp an r-value reference to v. This doesn't result in the move constructor or copy constructor being called, and nothing is pilfered.

飘落散花 2024-12-29 05:22:53
auto vp = move(v);

正在创建一个新的 vector 并调用它的移动构造函数:

vector(const vector<T>&& other);

这将窃取 v 的内容。

因此,“vp”的类型只是 vector .. 不涉及任何引用:

vector<int> vp;

它是“移动”内部结构 .. 而不是实际的向量本身。

因此 &vp 将与 &v 不同。但内容会移动。

auto vp = move(v);

Is creating a new vector<int> and calling it's move constructor:

vector(const vector<T>&& other);

Which will steal the contents of v.

So type of 'vp' is simply vector<int> .. no references involved:

vector<int> vp;

It's 'moving' the internals .. not the actual vector itself.

So &vp will be different to &v .. but the contents will move across.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文