Today, I came across a confusing compilation error with MS VC++ using a std::unique_ptr with a custom deleter. Here the code snippet:
|
#include <memory> #include <string> typedef void (*void_delete)(void *); template<typename T> void void_deleter(void * ptr) { delete reinterpret_cast<T*>(ptr); } std::unique_ptr<void, void_delete> f1() { return std::unique_ptr<void, void_delete>(new std::string("haha"), void_deleter); } std::unique_ptr<void, void_delete> f2() { return std::unique_ptr<void, void_delete>(nullptr); } |
The compiler complains : 12345678910111213c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2218): error C2338: unique_ptr constructed with null deleter pointer 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2214) : while compiling class template member function […]