Best way to combine C allocators and smart pointers?

Hi, I've come across a library that's written in C, and uses allocator and deallocator functions to allocate/deallocate structs. The functions are defined outside of the structs. I'd like to use smart pointers, as it's the c++ way.

One way which works would be
1
2
data* a_c = alloc_data();
std::unique_ptr<data, decltype(&dealloc_data)> a_cpp (a_c, dealloc_data);

but it seems odd to me. Is there some way to not have to declare a_c before a_cpp?
1
2
3
4
5
6
7
8
9
//Just to reduce boilerplate type declarations.
template <typename T, typename F>
std::unique_ptr<T, F> to_unique(T *p, const F &f){
    return std::unique_ptr<T, F>(p, f);
}

//...

auto a_cpp = to_unique(alloc_data(), &dealloc_data);
Perfect, thanks!
Topic archived. No new replies allowed.