4 ms·
A simple variable would be a scoped resource, unique_ptr can be transferred. { RAII file = ....; // do stuff } // file is no longer open
by kilotaras 5y ago
A simple variable would be a scoped resource, unique_ptr can be transferred.
{
RAII file = ....;
// do stuff
} // file is no longer open
vs
unique_ptr<RAII> outer_ptr;
{
unique_ptr<RAII> file = make_unique(...);
// do stuff
outer_ptr = file;
} // resource still open
- spacechild1 5y agoYour “RAII“ object can also be transferred if it supports move semantics (it really should). Both classes hold a unique resource (a file handle resp. a heap allocated object) that can be moved but not copied and the resource gets freed when the variable goes out of scope.
- cgrealy 5y agoYeah, I’m aware of that. I still don’t think it adds much to the complexity of unique_pet though. It’s certainly less complicated than its ancestor auto_ptr.