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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10319009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:53:37  来源:igfitidea点击:

unique_ptr<T> lambda custom deleter for array specialization

c++lambdac++11smart-pointersunique-ptr

提问by johnco3

I recently started porting lots of my existing C++ application code to over to C++11 and now that I am converting to the new smart pointers std::unique_ptrand std::shared_ptr, I have a specific question about custom deleters. I want to add a lambda logger to see where my deletes are being called but I cannot get the array specialization version to compile. Advice would be very much appreciated.

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

I have been searching in vain for an example of a custom deleter for array specialization unique_ptrfor VC++10or GCC 4.5.2+. I would like to print a log message when the deleters are called in a lambda - mainly to make sure that all the pointers that I think are going out of scope are doing so. Is this possible for the array version of the specialization? I can get it to work with the non array version, and I can also get it to work with an array specialization if I pass an external struct "MyArrayDeleter" as the second argument. One more thing, would it be possible to remove the ugly std::functionas I thought that I could let the lambda signature figure that out.

我一直在寻找用于VC++10GCC 4.5.2+ 的数组特化unique_ptr的自定义删除器的示例,但徒劳无功。我想在 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>          ]

回答by Managu

What about:

关于什么:

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

回答by zhaorufei

First of first, I use VC2010 with SP1, Mingw g++ 4.7.1

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

For array new, unique_ptr already support it in a clean way:

对于新数组,unique_ptr 已经以一种干净的方式支持它:

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

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

The output is:

输出是:

ctor
ctor
ctor
dtor
dtor
dtor

For customized deleter, unfortunately, it's inconsistent between VC2010 and g++:

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

VC2010:

VC2010:

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

g++:

克++:

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

The method by Managu is very well, because inline lambda is cool but hurt readability IMHO. It also emphsize that release resource before acquisition(RAII).

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

Here I suggest a declartive way to separate resource acquisition and release(Scope Guard, works for both VC2010 and g++ 4.7.1):

这里我建议采用声明式方式来分离资源获取和释放(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); } );

The point is you can get a resource in the old, clear way, and declare the statement to release the resource immediately following the resource-acquisition line.

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

The drawback is you cannot forward a single object around along with it's deleter.

缺点是您不能将单个对象与其删除器一起转发。

For FILE *, shared_ptr can be used as an alternative pointer for the same purpose(maybe a little heavy-weight, but works well for both VC2010 and g++)

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

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

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