How do I Write a Custom Deleter to Handle Forward Declared Types

删除回忆录丶 提交于 2020-01-06 08:22:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!