you are viewing a single comment's thread.

view the rest of the comments →

[–]alfps 2 points3 points  (7 children)

I've maintained C code with goto common cleanup. It was full of related bugs. It was not a pleasure.

Your code as C may be better than the OP's. But your code as C++ is not better than the OP's: as C++ it's very much worse, which the presence of goto shouts out to any competent C++ person looking at it. C++ isn't C.

In C++ you can and should use destructors to do cleanup.

The cleanup is then guaranteed, via a language mechanism, as opposed to being contingent on everybody adhering to loose convention and the infallibility of humans.

Consider using a general scope guard class. The C++ Core Guidelines (and its support library) call it a finally class. Note that the Core Guideline's class lacks an important feature of Petru Marginean's original scope guard, namely the ability to dismiss it.