unique_ptr <t>用于数组特化的lambda自定义删除器</t>

时间:2012-04-25 15:40:38

标签: c++ lambda c++11 smart-pointers unique-ptr

我最近开始将大量现有的C ++应用程序代码移植到C ++ 11,现在我正在转换为新的智能指针 std :: unique_ptr std :: shared_ptr ,我有一个关于自定义删除的具体问题。我想添加一个lambda记录器来查看我的删除被调用的位置但是我无法获得要编译的数组特化版本。建议将非常感谢。

我一直在搜索 VC ++ 10 GCC 4.5.2 + unique_ptr 的自定义删除器示例STRONG>。我想在lambda中调用删除器时打印一条日志消息 - 主要是为了确保我认为超出范围的所有指针都这样做。这可能是专业化的阵列版本吗?我可以使用非数组版本,如果我将外部结构“MyArrayDeleter”作为第二个参数传递,我也可以使用数组特化。还有一件事,是否有可能删除丑陋的 std :: function ,因为我认为我可以让lambda签名数字出来。

struct MySimpleDeleter {
    void operator()(int* ptr) const {
        printf("Deleting int pointer!\n");
        delete ptr;
    }
};
struct MyArrayDeleter {
    void operator()(int* ptr) const {
        printf("Deleting Array[]!\n");
        delete [] ptr;
    }
};
{
    // example 1 - calls MySimpleDeleter where delete simple pointer is called
    std::unique_ptr<int, MySimpleDeleter> ptr1(new int(5));

    // example 2 - correctly calls MyArrayDeleter where delete[] is called
    std::unique_ptr<int[], MyArrayDeleter> ptr2(new int[5]);

    // example 3 - this works (but default_delete<int[]> would have been passed
    // even if I did not specialize it as it is the default second arg
    // I only show it here to highlight the problem I am trying to solve
    std::unique_ptr<int[], std::default_delete<int[]>> ptr2(new int[100]);

    // example 3 - this lambda is called correctly - I want to do this for arrays
    std::unique_ptr<int, std::function<void (int *)>> ptr3(
        new int(3), [&](int *ptr){ 
            delete ptr; std::cout << "delete int* called" << std::endl; 
        });

    // example 4 - I cannot get the following like to compile
    // PLEASE HELP HERE - I cannot get this to compile
    std::unique_ptr<int[], std::function<void (int *)>> ptr4(
        new int[4], [&](int *ptr){  
            delete []ptr; std::cout << "delete [] called" << std::endl; 
        });
}

The compiler error is as follows:

The error from the compiler (which complains about the new int[4] for ptr4 below is:
'std::unique_ptr<_Ty,_Dx>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty,_Dx>'
1>          with
1>          [
1>              _Ty=int [],
1>              _Dx=std::tr1::function<void (int *)>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2513) : see declaration of 'std::unique_ptr<_Ty,_Dx>::unique_ptr'
1>          with
1>          [
1>              _Ty=int [],
1>              _Dx=std::tr1::function<void (int *)>
1>          ]

2 个答案:

答案 0 :(得分:36)

怎么样:

auto deleter=[&](int* ptr){...};
std::unique_ptr<int[], decltype(deleter)> ptr4(new int[4], deleter);

答案 1 :(得分:4)

首先,我使用VC2010和SP1,Mingw g ++ 4.7.1

对于array new,unique_ptr已经以干净的方式支持它:

struct X
{
    X()   { puts("ctor"); }
   ~X()   { puts("dtor"); }
};

unique_ptr<X[]>  xp(new X[3]);

输出结果为:

ctor
ctor
ctor
dtor
dtor
dtor

对于自定义删除器,遗憾的是,它在VC2010和g ++之间不一致:

VC2010:

  unique_ptr<FILE, function<void (FILE*)> > fp(fopen("tmp.txt", "w"), [](FILE *fp){
    puts("close file now");
    fclose(fp);
  });

克++:

  unique_ptr<FILE, void (*)(FILE*) > fp(fopen("tmp.txt", "w"), [](FILE *fp){
    puts("close file now");
    fclose(fp);
  });

Managu的方法非常好,因为内联lambda很酷但是可读性很差恕我直言。它还强调在获得之前释放资源(RAII)。

这里我建议一种分离资源获取和发布的声明方法(Scope Guard,适用于VC2010和g ++ 4.7.1):

template<typename T>
struct ScopeGuard
{
    T deleter_;
    ScopeGuard( T deleter) : deleter_(deleter) {}
    ~ScopeGuard() { deleter_() ; }
};
#define UNI_NAME(name, line) name ## line
#define ON_OUT_OF_SCOPE_2(lambda_body, line) auto UNI_NAME(deleter_lambda_, line) = [&]() {    lambda_body; } ; \
       ScopeGuard<decltype(UNI_NAME(deleter_lambda_, line))> \
       UNI_NAME(scope_guard_, line)  ( UNI_NAME(deleter_lambda_, line ));
#define ON_OUT_OF_SCOPE(lambda_body) ON_OUT_OF_SCOPE_2(lambda_body, __LINE__)

FILE * fp = fopen("tmp.txt", "w");
ON_OUT_OF_SCOPE( { puts("close file now"); fclose(fp); } );

关键是你可以用旧的,清晰的方式获取资源,并声明语句在资源获取线之后立即释放资源。

缺点是你无法转发单个对象及其删除。

对于FILE *,shared_ptr可以用作同一目的的替代指针(可能有点重量级,但对VC2010和g ++都很好)

shared_ptr fp2(fopen(“tmp.txt”,“w”),[](FILE * fp){fclose(fp); puts(“close file”);});