c++ - Usefulness of RAII without exceptions -
i found raii in c++ , examples of raii talk exception safety. how can release resources if exception thrown.
the question have is, if raii worth if not have exceptions turned on. in our firm work on embedded projects arm , exceptions turned off default , don't see need them.
thanks answers!
raii exceptions requirement.
raii without exceptions means can couple allocation of resources code dispose resources.
this lets have functions multiple exit points, simplifies writing of destructors (often destructors in raii heavy environment empty or default), can simplify object assignment , moving (once again, empty or default sufficient raii work).
a classic example embedded environments locking , unlocking mutex. want guarantee don't lock mutex , forget unlock it. this, code discipline means have have 1 exit point function, , have engage in gymnastics ensure happens.
with raii, create raii resource holder owns lock. can return whenever want, , code unlock resource automatically injected @ return site. code flow simplified, , resource leaks far less common.
raii amazing documentation. structure or class foo*
mean anything: how , when supposed deal resource unclear. structure or class std::unique_ptr<foo>
owning pointer. functions take std::unique_ptr<foo>
taking ownership on passed in pointer. functions return std::unique_ptr<foo>
giving ownership of pointer.
Comments
Post a Comment