I have several classes from a third-party library similar to the StagingConfigDatabase class, which requires destruction after it is created. I use shared_ptr for RAII, but would prefer to create shared_ptr using a single line of code , instead of using a separate template functor, as my example shows. Perhaps using lambda? or bind?
struct StagingConfigDatabase
{
static StagingConfigDatabase* create();
void destroy();
};
template<class T>
struct RfaDestroyer
{
void operator()(T* t)
{
if(t) t->destroy();
}
};
int main()
{
shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), RfaDestroyer<StagingConfigDatabase>());
return 1;
}
I considered something like:
shared_ptr<StagingConfigDatabase> pSDB(StagingConfigDatabase::create(), [](StagingConfigDatabase* sdb) { sdb->destroy(); } );
but this will not compile :(
Help!
source
share