是否有一个 boost 智能指针类可以配置为在销毁时不删除?

发布于 2024-07-27 12:15:16 字数 501 浏览 5 评论 0原文

我有一个智能指针列表。 我希望其中一些智能指针充当常规指针,这意味着它们只是对实例的引用,不参与其释放。 例如,它们可能指向堆栈上分配的实例。 列表中的其他智能指针应充当常规 boost::shared_ptr。

该类的外观如下:

template<class T> smart_ptr {
private:
    T *p;
    boost::shared_ptr<T> sp;
public:
    smart_ptr(T *p): p(p), shared(0) { } // p will not be deleted
    smart_ptr(boost::shared_ptr<T> &sp): p(sp.get()), sp(sp) { }
    T *get() const { return p; }
}

如果有一个 boost 类可以执行此操作,我更愿意使用它而不是自己编写一个类。 好像没有,还是我弄错了?

I have a list of smart pointers. I want some of these smart pointers to act as regular pointers, meaning they are simply a reference to an instance and are not involved in its deallocation. They might for example point to instances allocated on the stack. The other smart pointers in the list should act as regular boost::shared_ptr.

Here is how the class might look:

template<class T> smart_ptr {
private:
    T *p;
    boost::shared_ptr<T> sp;
public:
    smart_ptr(T *p): p(p), shared(0) { } // p will not be deleted
    smart_ptr(boost::shared_ptr<T> &sp): p(sp.get()), sp(sp) { }
    T *get() const { return p; }
}

If there is a boost class that does this, I would prefer to use it instead of writing a class myself. It appears there are none, or am I mistaken?

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

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

发布评论

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

评论(4

月隐月明月朦胧 2024-08-03 12:15:16

shared_ptr 的一个构造函数采用析构函数方法,并且您可以传入一个空函子。

在 boost::shared_ptr 中使用自定义Deallocator

(您只需要一个空函数。)

One constructor for shared_ptr takes the destructor method, and you can pass in an empty functor.

Using Custom Deallocator in boost::shared_ptr

(You want just an empty function.)

合约呢 2024-08-03 12:15:16

我的工具箱中有这个小类:

struct nodelete {
    template <typename T>
    void operator()( T * ) {}
};

用法:

int main() {
    SomeClass sc;
    boost::shared_ptr<SomeClass> p( &sc, nodelete() );
    // ...
}

I've got this little class in my toolbox for this:

struct nodelete {
    template <typename T>
    void operator()( T * ) {}
};

Usage:

int main() {
    SomeClass sc;
    boost::shared_ptr<SomeClass> p( &sc, nodelete() );
    // ...
}
失与倦" 2024-08-03 12:15:16

这听起来像是一个 boost::weak_ptr:
http://www.boost.org/doc/libs/ 1_35_0/libs/smart_ptr/weak_ptr.htm

但是您只能从shared_ptr 创建weak_ptr,因此对于堆栈分配的对象,我不确定这将如何工作。

This sounds like a boost::weak_ptr:
http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/weak_ptr.htm

But you can only create a weak_ptr from a shared_ptr, so as for your stack-allocated objects, I'm not sure how that would work.

臻嫒无言 2024-08-03 12:15:16

这闻起来有点糟糕的设计。

我想不出您不想删除指针的合理情况。 以下是(不合理的 IMO)情况:

1)静态持续时间对象。 相反,考虑使用单例混合(使用 CRTP 混合具有instance()方法的单例,该方法返回本地静态shared_ptr<>;本地静态是线程不安全的,因此您还需要一个适当的静态互斥体,如果这可以由多个线程调用)。 使用正确的单例的好处是,您的单例将在退出时在其他继续保留共享指针的对象之后被破坏。

2)在堆栈上创建的对象。 只是不要这样做。 相反,在受shared_ptr<>保护的堆上创建对象。 如果您需要在代码的不同部分为对象创建shared_ptr<>(即您不能从原始shared_ptr<>获取副本),则从boost::enable_shared_from_this<>继承; 并从shared_from_this()获取shared_ptr<>。

是否还有其他原因需要使用shared_ptr<> 永远不会删除任何东西?

This smells of bad design.

I can't think of a reasonable situation where you wouldn't want to delete the pointer. Here are the (unreasonable IMO) situations:

1) static duration objects. Instead, consider using a singleton mixin (use CRTP to mixin the singleton that has an instance() method that returns a copy of a local static shared_ptr<>; local statics are thread unsafe so you'll also need an appropriate static mutex if this could be called by multiple threads). The benefit of using a proper singleton is that your singleton will be destructed at exit after other objects that continue to hold shared_ptr<>'s to it.

2) objects created on the stack. Just don't do this. Instead create the object on the heap protected by a shared_ptr<>. If you need to create shared_ptr<>'s to the object in different parts of the code (i.e. you can't take copies from an original shared_ptr<>) then inherit from boost::enable_shared_from_this<> and get shared_ptr<>'s from shared_from_this().

Is there some other reason you want a shared_ptr<> that doesn't ever delete anything?

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