Unique_ptr pointer deleter pointer to pointer

I am trying to map a third-party API using a unique unique_ptr decoder. The problem is that the API is like this:

x *x_alloc_x(void);

void x_free_x(x **p);

The API wants me to provide a pointer to a pointer so that it sets to NULL. I wrote my deleter functor as a translator-pointer, which I convert to a pointer to a pointer using the & operator.

struct XDeleter {
    void operator(x *&p) { x_free_x(&p); }
};

This works with GCC 4.6, but is it really allowed by the standard? If not, is there a standard way to map this API to a deleter?

+4
source share
1 answer

The code is not correct. n3797 [unique.ptr.single.dtor] / 1 requires in the destructor unique_ptr:

: get_deleter()(get()) [...]

[unique.ptr.single] :

pointer get() const noexcept;

, get() prvalue.

, [unique.ptr.single]/1

D [ ] , lvalue-reference lvalue-reference , D D ptr unique_ptr<T, D>::pointer, d(ptr) , .

- prvalue.


Mooing Duck:

struct XDeleter {
    void operator(x *p)
    { x_free_x(&p); }
};

unique_ptr, , nullptr .

+2

Source: https://habr.com/ru/post/1539072/


All Articles