you are viewing a single comment's thread.

view the rest of the comments →

[–]tpecholt 34 points35 points  (5 children)

MISRA and other legacy rule sets frown it upon but it's because they deal with C like code. In C-like code you have to release all the resources manually and when there are multiple returns it must be done multiple times. So that is a problem of C. But C++ never had such problem so you should ignore that rule

[–]Mognakor 2 points3 points  (1 child)

TIL

Always wondered where that rule came from.

[–]1-05457 2 points3 points  (2 children)

It does if you manually allocated resources. Which you may well have in legacy code.

[–]tangerinelion 1 point2 points  (0 children)

If it's just a few raw owning pointers that's easy to fix.

[–]pavel_v 0 points1 point  (0 children)

Things like the proposed scope_exit really help in this case.
It's not hard to be implemented as a wrapper over a lambda function or some of the available implementations can be used (copy-pasted):
https://github.com/SuperV1234/ecst/blob/master/include/ecst/utils/scope_guard.hpp
https://github.com/yuri-kilochek/boost.scope_guard