问题
Some older versions of visual-studio required a complete definition of the type for default_deleter
to be defined. (I know this was the case in Visual Studio 2012 but I'd give bonus points to anyone who could tell me which version remedied this.)
To work around this I can write a custom_deleter
function like so:
template <typename T>
void custom_deleter(T* param) {
delete param;
}
And declare a smart pointer on a forward declared type like so: unique_ptr<Foo, function<void(Foo*)>> pFoo
, assigning this pointer where Foo
was completely declared like so: pFoo = decltype(pFoo)(new Foo, function(custom_deleter<Foo>))
Now as suggested here, I'd like to make my custom_deleter
into a functor like this:
struct MyDeleter {
template <typename T>
void operator ()(T* param) {
delete param;
}
};
Which should allow me to declare: unique_ptr<Foo, MyDeleter>
and assign: pFoo = decltype(pFoo)(new Foo)
However when I try to do this in visual-studio-2012 I get:
warning C4150: deletion of pointer to incomplete type
Foo;
no destructor called
Is there a way I can work around this as well?
Live Example
来源:https://stackoverflow.com/questions/58011898/how-do-i-write-a-custom-deleter-to-handle-forward-declared-types