Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] Is there any interest in unique_ptr with type erased deleter?
From: Andrey Davydov (andrey.a.davydov_at_[hidden])
Date: 2017-03-21 08:46:08


On Tue, Mar 21, 2017 at 11:10 AM, TONGARI J via Boost <boost_at_[hidden]
> wrote:

> 2017-03-21 15:04 GMT+08:00 Andrey Davydov via Boost <boost_at_[hidden]
> >:
> [...]
>
> > I have implemented smart pointer with semantics of unique ownership and
> > type erased deleter (https://github.com/AndreyG/unnamed). It has size
> of 3
> > pointers and it doesn't require additional memory allocations for the
> case
> > when deleter is empty class (for example, std::default_delete or
> > non-capturing lambda) and for the case when deleter is known at compile
> > time function (for instance, fclose). The are some simple examples of
> usage
> > and tests in the repo.
> >
> > Why don't just use unique_ptr<T, function<void (void *>>? The main reason
> > is that due to the small object optimization std::function has rather big
> > size. For instance, sizeof(unique_ptr<T, function<void (void *>>) == 72
> for
> > MSVC x64! Results for the other platforms can be found in the repo
> README.
> >
> > Does it seem useful?
> >
>
> Why not:
> ```
> template<class T>
> using my_ptr = unique_ptr<T, void(*)(T*)>;
> ```
> ? Even if you need a stateful deleter, you don't need to reinvent the whole
> unique_ptr, just use your custom type-erased deleter.
>

Because I'd like, for example, cast my_ptr<Derived> to my_ptr<Base>.

-- 
Andrey Davydov

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk