如何转发声明自定义unique_ptr

use*_*708 2 c++ forward-declaration unique-ptr

我在实现如下构造时遇到了问题.我的想法是我在一些头文件中定义了一个实用程序函数,它分配了某种资源.为了减轻客户自己释放它的必要性,我想把它包装成一个智能指针.不幸的是,我不得不将删除器类型提供给客户端,并且无法正确地向前声明它.我当前的解决方案看起来像这样,导致多个定义错误,因为删除器是使用utilities.h的每个附加包含重新定义的

这是我正在寻找的优雅解决方案?

// utilities.h
namespace foo
{
  auto del = []( MyResource* res) { Deallocate( res ); };
  std::unique_ptr<MyResource, decltype(del)> getResource( );
}

// utilities.cpp
namespace foo
{
  std::unique_ptr<MyResource, decltype(foo::del)> getResource( )
  {
    return std::unique_ptr<MyResource, decltype(foo::del)>( Allocate( ), del );
  }
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*ica 5

使用普通类而不是lambda:

标题:

namespace foo {

  struct Deleter {
    void operator() (MyResource *res) const { Deallocate(res); }
  };

  std::unique_ptr<MyResource, Deleter> getResource();
}
Run Code Online (Sandbox Code Playgroud)

资源:

namespace foo
{
  std::unique_ptr<MyResource, decltype(foo::del)> getResource( )
  {
    return std::unique_ptr<MyResource, Deleter>( Allocate( ) );
  }
}
Run Code Online (Sandbox Code Playgroud)

这具有额外的优点,即在创建指针时不必指定删除器 - 默认构造Deleter就可以了.