std :: unique_ptr的非所有权副本 [英] Non-ownership copies of std::unique_ptr

查看:72
本文介绍了std :: unique_ptr的非所有权副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个容器:资源的所有者和非所有者.因为我只有1个所有者,所以我想我需要unique_ptr.

There's two containers: owner and non-owner of resource. As I have only 1 owner, I suppose I need unique_ptr.

class OwnershipContainer {
public:
    void add(std::unique_ptr<Obj> obj) {
        objects.push_back(std::move(obj));
    }
    Obj* get(std::size_t i) { return objects[i].get(); }
private:
    std::vector<std::unique_ptr<Obj>> objects;
};

非所有者容器必须使用哪种指针?首先想到的是原始指针.但是我不能保证Obj的寿命匹配或超过非所有者容器的寿命.

What of kind of pointer i have to use for non-owner container? First thought was raw pointer. But i cannot give guarantee that lifetime of Obj match or exceed lifetime of Non-owner container.

class NonOwnershipContainer {
public:
    void add(Obj *obj) {
        objects.push_back(obj);
    }
    Obj* get(std::size_t i) { return objects[i]; }
private:
    std::vector<Obj*> objects;
};

int main() {
    NonOwnershipContainer nonOwnershipContainer;
    {
        OwnershipContainer ownershipContainer;
        ownershipContainer.add(std::make_unique<Obj>(1));

        nonOwnershipContainer.add(ownershipContainer.get(0));
    }
    auto pobj = nonOwnershipContainer.get(0); // dangling pointer
}

我可以对所有者使用shared_ptr,对非所有者使用weak_ptr,因此我可以检查weak_ptr是否过期.但是shared_ptr表示我拥有共享所有权,在我的情况下是不正确的,并且我不需要引用计数器.

I could use shared_ptr for owner and weak_ptr for non-owner, so I could check if weak_ptr is expired or not. But shared_ptr means that i have shared ownership, that's not true in my case, and I no need reference counter.

我不打算延长寿命.当所有者容器被破坏时,我想避免悬空的指针.就像我在上面写的,我可以使用shared_ptr + weak_ptr.

I'm not trying to extend lifetime. When owner container is destroyed, I want to avoid dangling pointers. As I wrote above, I could use shared_ptr + weak_ptr.

class OwnershipContainer {
public:
    void add(std::shared_ptr<Obj> obj) {
        objects.push_back(obj);
    }
    std::shared_ptr<Obj> get(std::size_t i) { return objects[i]; }
private:
    std::vector<std::shared_ptr<Obj>> objects;
};


class NonOwnershipContainer {
public:
    void add(std::shared_ptr<Obj> obj) {
        objects.push_back(obj);
    }
    std::shared_ptr<Obj> get(std::size_t i) { return objects[i].lock(); }
private:
    std::vector<std::weak_ptr<Obj>> objects;
};

int main() {
    NonOwnershipContainer nonOwnershipContainer;
    {
        OwnershipContainer ownershipContainer;
        ownershipContainer.add(std::make_shared<Obj>(1));

        nonOwnershipContainer.add(ownershipContainer.get(0));
    }
    auto pobj = nonOwnershipContainer.get(0); // no more dangling pointer, pobj == nullptr
}

但是在这种情况下,我为参考计数器付款,这在哲学上是错误的:只有一个所有者使用shared_ptr.

But in this case, I pay for reference counter, and it is philosophically wrong: with only one owner use shared_ptr.

推荐答案

您实际上拥有共享的所有权:通过 NonOwningContainer (包含指针)访问对象时,诸如此类的东西,您必须拥有所有权,否则在使用该对象时该对象可能会从您的身下消失.

You actually do have shared ownership: When you access the object via the NonOwningContainer-contained pointer-like thing, you must take ownership, or the object may disappear out from under you while you're working with it.

由于您不能保证该物体不会从您的身下消失:

Since you can't guarantee the object won't disappear out from under you:

但是我不能保证Obj的寿命匹配或超过非所有者容器的寿命.

But i cannot give guarantee that lifetime of Obj match or exceed lifetime of Non-owner container.

然后,您唯一的选择是共享所有权.因此, shared_ptr weak_ptr 是合适的方法.

then your only option is to share ownership. Hence shared_ptr and weak_ptr is the appropriate approach.

此外,根据 OwnershipContainer NonOwnershipContainer 的生​​命周期的不同,请注意

Also, depending on the difference in lifetime of OwnershipContainer and NonOwnershipContainer, be aware of the interaction between std::make_shared and std::weak_ptr.

这篇关于std :: unique_ptr的非所有权副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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