推入std :: vector时std :: unique_ptr的正确用法是什么 [英] What is the correct usage of std::unique_ptr while pushing into std::vector

查看:601
本文介绍了推入std :: vector时std :: unique_ptr的正确用法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个指向班级中对象的指针的矢量。为了避免为此构造析构函数,我想使用 std :: unique_ptr ,因为在类中创建/拥有/破坏了对象,但是我遇到了编译器错误不明白下一个代码将作为我的问题的简短示例:

I want to have a vector of pointers to objects in my class. To avoid making a destructor for it I wanted to use std::unique_ptr, as the objects are created/owned/destructed in my class, but I have a compiler error I can't understand. Next code will serve as a short sample for my problem:

std::unique_ptr<int> createPtr(int value)
{
    std::unique_ptr<int> ptr(new int(value));
    return ptr;
};

int main()
{
    std::vector<std::unique_ptr<int>> vec;
    vec.push_back(createPtr(1));

    std::unique_ptr<int> ptr = createPtr(2);
    vec.push_back(ptr);//error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
}

能否请您解释一下我为什么收到此错误,以及<$ c $的正确用法是什么c> std :: unique_ptr ?

Can you please explain me why I get this error and what is the correct usage for std::unique_ptr?

推荐答案

请考虑 vec.push_back ()。它有两个重载,它们采用 const std :: unique_ptr< int>& std :: unique_ptr< int>&& 。第一次永远不会使用。这是因为 vector<> 要求类型是可分配的或可移动的(添加C ++ 11)。 可分配性指的是复制。 push_back(const T&)将尝试将传入的值复制(分配)到容器末尾的新空间。 std :: unique_ptr<> 表示由单个所有者拥有的资源。通过复制它(指针),将有多个所有者。因此, unique_ptr 是不可复制的

Consider the vec.push_back(). It has two overloads which take either const std::unique_ptr<int>& or std::unique_ptr<int>&&. The first overload can never be used. This is because the vector<> requires the type to be assignable, or movable (C++11 addition). 'Assignability' implies copying. push_back(const T&) will try to copy (assign) the incoming value to the new space at the end of the container. std::unique_ptr<> represents a resource that is owned by a single owner. By copying it (the pointer) multiple owners would be present. Because of that unique_ptr is not copyable.

说了所有这些,您只能使用 T& 重载。

Having said all of it, you can only use the T&& overload.

createPtr()返回 std :: unique_ptr< int> ,但是由于这是一个临时结果(函数返回值),因此被视为右值引用(隐式)。这就是为什么可以使用它的原因。

createPtr() returns std::unique_ptr<int>, but as this is a temporary result (function return value) it is considered a rvalue reference (implicitly). That is why this can be used.

ptr 只是 std :: unique_ptr< ; int> 是左值引用(无论您是否在其旁边放置&& ;,因为已命名右值仍然被视为左值)。左值永远不会隐式转换为右值(完全不安全)。但是您基本上可以通过使用 std :: move()告诉编译器好吧,您可以接受我传递给您的对象,并且保证不会将参数保留完整。 code>。

ptr is just a std::unique_ptr<int> which is a lvalue reference (no matter if you put && next to it, as named rvalues are still treated as lvalues). Lvalue is never implicitly converted to rvalue (completely unsafe). But you can basically tell the compiler "Ok, you can take the object I pass you, and I promise I won't expect the argument to be left intact" by using std::move().

这篇关于推入std :: vector时std :: unique_ptr的正确用法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆