Re:The Next Steps for Single Ownership and RAII
by verdagon on Saturday July 18, 2020 @01:42PM
(#23883132)
Great question! We could also use a deleter, set up when we create the object, but thats often too early to know what parameters to pass into the destructor.
In some cases, you do know the destruction parameters up-front, and there I would agree that a deleter is great.
Sometimes you can't know the parameters up front, so can't easily get them into the deleter when you initialize. The call-once std::function and the future are good examples: These both take parameters which you might not know until later on. If you already know the values of a future when you're creating it, it probably wouldn't be a future.
future.~resolve(resultOfSomeAsynchronousCalculation);
Er, why wouldn't you just use automatic storage duration? That would look even simpler than the Vale implementation and have the same benefits:
You could of course explicitly write the constructor if you choose.> Destructor Parameters
This can be done in C++: both unique and shared ptrs can take a deleter instance. So for the rollback example:
But my question is -- why? At this point you are losing one of the main benefits of RAII IMO: scheduling cleanup work right next to initialization work, allowing you to assume it is taken care of as you read the rest of the function:Transaction tx(conn, Transaction::ROLLBACK_MODE_TUMBLE);
...
if (ok) { tx.commit(); } return;
Similarly with the Future example -- IMO this is far clearer:
where AutoRun is something simple like