如何将删除器传递给由shared_ptr持有的同一类中的方法

发布于 2024-10-08 15:46:33 字数 810 浏览 0 评论 0原文

我有几个来自第三方库的类,类似于 StagingConfigDatabase 类,它需要在创建后销毁。我正在为 RAII 使用shared_ptr,但更愿意使用单行代码创建shared_ptr,而不是像我的示例所示使用单独的模板函子。也许使用 lambda 表达式?或绑定?

struct StagingConfigDatabase
{
  static StagingConfigDatabase* create();
  void destroy();
};

template<class T>
    struct RfaDestroyer
    {
        void operator()(T* t)
        {
            if(t) t->destroy();
        }
    };

    int main()
    {
      shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), RfaDestroyer<StagingConfigDatabase>());
    return 1;
    }

我正在考虑类似的事情:

shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), [](StagingConfigDatabase* sdb) { sdb->destroy(); } );

但这不能编译:(

救命!

I have several classes from 3rd party library similar to the class, StagingConfigDatabase, which requires to be destroyed after it is created. I am using a shared_ptr for RAII but would prefer to create the shared_ptr using a single line of code rather than using a seperate template functor as my example shows. Perhaps using lambdas? or bind?

struct StagingConfigDatabase
{
  static StagingConfigDatabase* create();
  void destroy();
};

template<class T>
    struct RfaDestroyer
    {
        void operator()(T* t)
        {
            if(t) t->destroy();
        }
    };

    int main()
    {
      shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), RfaDestroyer<StagingConfigDatabase>());
    return 1;
    }

I was considering something like:

shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), [](StagingConfigDatabase* sdb) { sdb->destroy(); } );

but that doesn't compile :(

Help!

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

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

发布评论

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

评论(2

翻身的咸鱼 2024-10-15 15:46:33

我假设 createStagingConfigDatabase 中是静态的,因为如果没有它,您的初始代码将无法编译。关于销毁,您可以使用简单的 std::mem_fun< /a>:

#include <memory>

boost::shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), std::mem_fun(&StagingConfigDatabase::destroy));

I'll assume that create is static in StagingConfigDatabase because your initial code wouldn't compile without it. Regarding destruction, you can use a simple std::mem_fun :

#include <memory>

boost::shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), std::mem_fun(&StagingConfigDatabase::destroy));
陈甜 2024-10-15 15:46:33

你使用什么编译器?它是否支持 lambda 等 C++0x 功能?以下内容(与您的示例基本相同)在 MSVC 2010 下编译并工作正常:

#include <iostream>
#include <memory>

struct X
{
    static X *create()
    {
        std::cout << "X::create\n";
        return new X;
    }

    void destroy()
    {
        std::cout << "X::destroy\n";
        delete this;
    }
};

int main()
{
    auto p = std::shared_ptr<X>(X::create(), [](X *p) { p->destroy(); });
    return 0;
}

“工作正常”,我的意思是“输出 X::create 后跟 X::destroy”。

What compiler are you using? Does it support C++0x features like lambdas? The following (which is basically the same as your example) compiles and works fine for me under MSVC 2010:

#include <iostream>
#include <memory>

struct X
{
    static X *create()
    {
        std::cout << "X::create\n";
        return new X;
    }

    void destroy()
    {
        std::cout << "X::destroy\n";
        delete this;
    }
};

int main()
{
    auto p = std::shared_ptr<X>(X::create(), [](X *p) { p->destroy(); });
    return 0;
}

By "works fine", I mean "outputs X::create followed by X::destroy".

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