What would be the most concise way to pass unique_ptr a custom deleter that does nothing? I need for a JNI function I'm writing, where the C++ side expects a unique_ptr, BUT, I don't want the object held by the unique_ptr to be deleted upon exiting the JNI function - I take care of the deletion later. So I'd like to do something like:
std::unique_ptr<MyClass, noop_delete> ptr;
In one line - not with a separate function definition :-)
As @101010 pointed out, that's very strange to have a
std::unique_ptr
with a nop deleter, since the only valuable thingstd::unique_ptr
has is actually the deleter. Also, you said that "C++ side expects a unique_ptr", but astd::unique_ptr
with a different deleter would be a different type, and this may not work.Nevertheless, here's the way to do it:
Note that this nop type can be used as no-operation anywhere in place of a one-argument functor.