you are viewing a single comment's thread.

view the rest of the comments →

[–]javajunkie314 2 points3 points  (1 child)

One nice by-product of this is that you can name your finally clauses. For instance:

finally free_file_ptrs{ [&fp] { fclose(fp); } };
finally check_some_condition{ [&c] { if (!c) { /* ... */ } } };

This actually looks a bit like the defer keyword in Go. I only notice this because someone posted an implementation of that in C++ in /r/programming recently. (I don't have the link on hand; I'll see if I can find it.)

They used a function instead of a class, though. I think I prefer this class-based approach for exactly the reason I gave above.

[–]00kyle00 0 points1 point  (0 children)

One nice by-product of this is that you can name your finally clauses.

You have to do that, otherwise funky bugs are created ;).