Reputation: 1763
I have a class member that is std::unique_ptr with a custom deleter. The custom deleter is a lambda function around a c function from an external library I do not control. I want to alias the unique_ptr type so that clients don't have to be exposed to the full signature. That is, I want :
struct {
mytype_ptr foo;
}
instead of:
struct {
std::unique_ptr<mytype, lambda<blah>> foo;
}
I tried doing this in the class header file:
auto x = [](mytype* t){ mytype_destroy(t); };
using mytype_ptr = std::unique_ptr<mytype, decltype(x)>;
but of course the linker complains about multiple definitions.
I have tried various permutations, including inlining the lambda definition in decltype() and others, without success.
Is there a clean way to do this?
Upvotes: 0
Views: 125
Reputation: 473946
No, you cannot. lambda<blah>
is not an implementation detail of the object; it is part of the object. The deleter of a unique_ptr
is part of its type. And this type is a member of the object. So any external users of that type will have to know about all of its members.
Unless you use PIMPL or type erasure (via std::shared_ptr
, for example) or something similar, external users will need to access the type.
If all you want to do is keep users from having to know about the C API, you can write a simple function declaration or functor in the header, and put the implementation of its deletion operation in a source file:
//In header
struct my_deleter
{
void operator()(mytype*) const;
};
Upvotes: 2