I want to do automatic cleanup using std::unique_ptr but to initialize the unique_ptr I need an address to point to. How can I avoid the noUse variable?
bool noUse;
auto deleter = [](bool *){ DESTROY_SOMETHING };
std::unique_ptr<bool, decltype(deleter)> upp(&noUse, deleter); // I want to avoid the usage of this extra noUse variable
START_SOMETHING
//COMPLEX IF ELSE RETURN LOGIC
**UPDATE: ** I finally resorted to this code. Thanks everyone for quick responses.
auto deleter = [](void *){ DESTROY_SOMETHING };
std::unique_ptr<void, decltype(deleter)> upp(nullptr, deleter);
if(enabled)
{
//START_SOMETHING
upp.reset(reinterpret_cast<void *>&upp);
}
//COMPLEX IF ELSE RETURN LOGIC
As long as the custom deleter doesn't require
noUseto have any specific value, you can use some arbitrary integer literal (other than zero, that would make it a null pointer):This is a bit of a hack, and there are better solutions, such as
std::experimental::scope_exit. It also doesn't make much sense that this is using abool*, why not just avoid*if we don't care about the type?You can also build your own solution, see:
@Joe has written a solution very similar to
std::experimental::scope_exitwhich does this.Note 1: conversion of
1tobool*has implementation-defined effect ([expr.reinterpret_cast]), using this pointer is implementation-defined ([basic.stc.general]), and comparison of the pointer tonullptris implementation-defined, though never UB ([expr.eq]).Note 2: Developers commonly create a local
staticvariable and take its address when they need astd::unique_ptras a "scope exit object". This is the most robust option.