unique_ptr<T> lambda custom deleter for array specialization [duplicate]

蹲街弑〆低调 提交于 2019-11-30 04:24:57
Managu

What about:

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

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

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

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:

  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).

Here I suggest a declartive way to separate resource acquisition and release(Scope Guard, works for both VC2010 and 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++)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!