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

joh*_*co3 30 c++ lambda smart-pointers unique-ptr c++11

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

我已经白白被搜索用于阵列专业化一个定制删除的一个例子的unique_ptrVC++ 10GCC 4.5.2+.我想在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>          ]
Run Code Online (Sandbox Code Playgroud)

Man*_*agu 38

关于什么:

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

  • 在C++中,`int`s的动态数组表示为`int*`; 即,`new int [4]`的类型是`int*`.因此,删除器需要一个`int*` - 为什么额外的间接级别是有用/需要的?通过实际尝试_compile_您的代码可以很容易地证明这一点..:[不起作用](http://ideone.com/fujAk),[确实有用](http://ideone.com/JeXYD).在任何情况下都不是`int(*ptr)[]`有效的语法; 至少它必须是`int(*ptr)[N]`用于某些已知的`N`,作为指向_statically_ size数组的指针. (4认同)
  • 你的删除器的参数类型是错误的 - 应该是`int*ptr`而不是`int(*ptr)[]`. (2认同)

归档时间:

查看次数:

24125 次

最近记录:

7 年,11 月 前