I've got a C code, working with some resources. It has functions like
ResourcePointer resource_new(void);
void resource_delete(ResourcePointer *res);
where ResourcePointer
is
typedef void * ResourcePointer;
I would like to create a typedef
for std::unique_ptr
, specifying that custom default deleter.
The following works, but requires repeating resource_delete
.
typedef std::unique_ptr<std::remove_pointer<ResourcePointer>::type,
void(*)(ResourcePointer)> Resource_auto_pointer;
and later in the code
Resource_auto_pointer resource(resource_new(), resource_delete);
...
Resource_auto_pointer res2 = { resource_new(), resource_delete };
How should I change typedef
, so that compiler would automatically substitute resource_delete
every time it is needed?
I want my code to look like the following
Resource_auto_pointer2 resource (resource_new());
...
Resource_auto_pointer2 res2 = { resource_new() };
The compiler should somehow guess that it should call resource_delete
for each object of type Resource_auto_pointer2
.
I work in MS Visual Studio 2013.
Update I've read answers to other similar questions. I don't understand two things.
- Why std::function doesn't work?
- why should I create new types, since (presumably) everything is said already?