lundi 14 décembre 2020

Can I succintly declare std::unique_ptr with custom deallocator?

I am dealing with C types which have a new_ and free_ methods associated.

The type signature of new_ may vary, but the free_ is always a void free_something(void);

Currently, I am declaring my unique_ptrs this way, which seems overly verbose:

std::unique_ptr<qdr_link_t, decltype(&free_qdr_link_t)> link{new_qdr_link_t(), free_qdr_link_t};

Can I do this with less ceremony, somehow? I saw a neat solution for a situation when my deallocator is the std::free() function, at https://stackoverflow.com/a/43626234/1047788. I tried to create a version where I could parameterize the deallocator, but I got nowhere with it.

The best I could think of was to create a macro from the above declaration.

Aucun commentaire:

Enregistrer un commentaire