2017-06-15 1:28 GMT+02:00 Gavin Lambert via Boost
On 15/06/2017 07:52, Richard Hodges wrote:
If developers do not understand RAII, then an afternoon of training can solve that.
RAII is the foundation of correct c++. It is the fundamental guarantee of deterministic object state. A program without RAII is not worthy of consideration. The author may as well have used C.
Perhaps there is an argument that says that RAII adds overhead to a program’s footprint. If things are that tight, fair enough.
Given that RAII is so fundamental to exception safety, it's surprising that there isn't a vocabulary execute-lambda-inside-destructor type (eg. "guard") in the STL, to replace cleanup/try-finally style code.
Granted, it's simple to write one yourself, but that seems like a poor rationalisation to omit it, especially once lambdas became standard.
I suspect that the lack of such a type is probably a significant reason exception-unsafe code ends up surviving -- it's more effort to write RAII wrappers for code that you're not really expecting to encounter exceptions in, even though you should.
If you allow arbitrary lambdas to be called at the end of the scope, this itself causes many bugs: 1. They have access to scope variables that might already have been destroyed when the lambda is executed. It is easy to overlook it. (This is a no-problem for destructors, because they do not see the context in which they are called.) 2. People will start calling a potentially throwing lambdas, which may result in double-exception problem. (This is not a problem in Java-like languages, where you simple ignore some errors.) Regards, &rzej;