A possible defer mechanism for C

Next big thing signI remember that some other programming language (possibly Go) has a defer mechanism. You can tell it to defer a function call until the end of the function. I think this means, even if you do an early return that all functions that have been deferred will still run.  It’s a handy language feature. Alternatives to it ion say C# are try … finally where the finally ensures that code gets runs.

The reason for mentioning this is it’s a possibility for a future inclusion in the next C standard.  There’s a quite long article about defer listing benefits. But even better the author Jens Gustedt has provided a reference implementation of it on GitLab.

I’m all in favour of this. Adding new features to C is not something to be done lightly but error checking and handling in C has always been a bit iffy. I’ve never used a SetJmp for example.  I do hope that defer gets added to C though I can’t see it being there for a few years.