带有复制构造函数的Emplace和Try_emplace

发布于 2025-01-29 08:30:10 字数 1005 浏览 3 评论 0原文

我对EMPLACE和TRY_EMPLECE有一个问题,因为他们在移动对象时始终使用复制构造函数

#include <iostream>
#include <unordered_map>
#include <map>

using namespace std;

class Too {
public:
     Too(int x, int y):x_(x), y_(y) {
        cout << "init " << x_  << endl;
    }
    Too(const Too& too):x_(too.x_+1), y_(too.y_+1) {
        cout << "Copy happen: x = " << x_ << endl;
     }
    ~Too() {
        cout << "delete too " << x_ << endl;
    }
private:
    int x_, y_;
};



std::map<int, Too> v;


int main()
{
    v.emplace(100, Too{ 100,23 });

    v.try_emplace(12, 12, 13);

    Too t = Too(10, 11);
    v.try_emplace(11, std::move(t));
}


。 初始化100 复制发生:x = 101 也删除100 初始化12 初始化10 复制发生:x = 11 也删除10 也删除101 也删除12 如您所见,删除11


,只有v.try_emplace(12,12,13)不使用复制构造函数。 V.Emplace(100,Too {100,23})和v.try_emplace(11,std :: move(t))调用复制构造函数。

那么,即使我使用std :: Move(t),该怎么办?

任何建议都将不胜感激。

谢谢,

I have an issue with emplace and try_emplace as they always use the copy constructors when moving an object in.

#include <iostream>
#include <unordered_map>
#include <map>

using namespace std;

class Too {
public:
     Too(int x, int y):x_(x), y_(y) {
        cout << "init " << x_  << endl;
    }
    Too(const Too& too):x_(too.x_+1), y_(too.y_+1) {
        cout << "Copy happen: x = " << x_ << endl;
     }
    ~Too() {
        cout << "delete too " << x_ << endl;
    }
private:
    int x_, y_;
};



std::map<int, Too> v;


int main()
{
    v.emplace(100, Too{ 100,23 });

    v.try_emplace(12, 12, 13);

    Too t = Too(10, 11);
    v.try_emplace(11, std::move(t));
}


output
init 100
Copy happen: x = 101
delete too 100
init 12
init 10
Copy happen: x = 11
delete too 10
delete too 101
delete too 12
delete too 11


As you can see, only v.try_emplace(12, 12, 13) do not use the copy constructor.
both v.emplace(100, Too{ 100,23 }) and v.try_emplace(11, std::move(t)) invoke the copy constructor.

So how can it be even when I use std::move(t)?

Any suggestion would be highly appreciated.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

很酷不放纵 2025-02-05 08:30:10

由于您已经为您的课程提供了一个复制构造函数,因此MOVE构造函数也:: Too(Too&amp;&amp;) Will 不会由编译器隐式生成

此外,如果没有可用于类的移动构造函数,则可以使用复制构造函数。


为了使用移动构造函数,您必须明确提供适当的用户定义的移动构造函数to :: Too(To to&amp;&amp;),那么您将获得所需的结果。

您可以通过添加(又有&amp;&amp;)=默认值;或编写自己的移动构造函数来添加MOVE构造函数,该构造构建器将在构造函数初始化器列表中执行初始化。

Since you've provided a copy constructor for your class, the move constructor Too::Too(Too&&) will not be implicitly generated by the compiler.

Moreover, when there is no move constructor available for a class, the copy constructor can be used.


For using the move constructor you have to explicitly provide an appropriate user-defined move constructor Too::Too(Too&&), then you will get the desired result.

You can add the move constructor either by adding Too(Too&&) = default; or writing your own move constructor which will do the initialization in the constructor initializer list.

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