Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] Is there any interest in unique_ptr with type erased deleter?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2017-03-21 14:33:26


On 03/21/17 16:33, Andrey Davydov wrote:
> On Tue, Mar 21, 2017 at 4:11 PM, Andrey Semashev
> <andrey.semashev_at_[hidden] <mailto:andrey.semashev_at_[hidden]>> wrote:
>
> struct poly_deleter
> {
> template< typename T >
> poly_deleter(T* p) : m_p(p), m_delete(&poly_deleter::do_delete< T >)
> {
> }
>
> void operator() (void*) const noexcept
> {
> m_delete(m_p);
> }
>
> private:
> template< typename T >
> static void do_delete(void* p) noexcept
> {
> delete static_cast< T* >(p);
> }
>
> void* m_p;
> void (*m_delete)(void*);
> };
>
> This will work even if you form std::unique_ptr<void, poly_deleter>
> (i.e. don't have a universal base class).
>
> Your poly_deleter implementation is the same as I used inside my smart
> pointer implementation, but there is an issue with std::unique_ptr<T,
> poly_deleter>, namely it is not constructible from std::unique_ptr<T,
> std::default_delete<T>> because poly_deleter must capture pointer and so
> it is not constructible from std::default_delete.

I think that problem can be solved by make_poly_unique or a conversion
helper function. Hardly a whole new smart-pointer is an adequate solution.


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