Defer Reference Implementation for C
Is this a serious proposal for a new C language feature? Or is this just an experiment from someone&#x27;s masters thesis or something? The paper is titled &quot;Proposal for C2x&quot;, but this can&#x27;t possibly be seriously considered. I have so many questions.<p>In section 1.1, the linearization it gives with goto statements is barely longer than the defer example. They claim defer is better just because of the proximity of the cleanup code? Why not just move the &quot;resources acquired&quot; code to a separate function? You wouldn&#x27;t even need goto in that case, you could just nest if statements to do the cleanup.<p>The spec claims defer allocates memory. Why? As far as I know __attribute__((cleanup(fn))) doesn&#x27;t allocate memory. This defer may exhaust memory, and if so, it will immediately terminate execution of the enclosing guard block with a panic() and DEFER_ENOMEM. So like an exception?<p>This says exit() or panic() will clean up all guarded blocks across all function calls of the same thread. So basically stack unwinding? Apparently you can recover somewhere with a call to recover()? This is just exceptions by another name. This stack unwinding can&#x27;t possibly interoperate with existing code that expects error return values.<p>This claims it&#x27;s robust because any deferred statement is guaranteed to be executed eventually, and it describes in great detail how it runs defer statements on signals. What if I write an infinite loop, or get a SIGKILL, or yank the power cord? Obviously deferred statements won&#x27;t be executed.<p>This says defer is implemented with longjmp. Isn&#x27;t setjmp&#x2F;longjmp way too slow for exception handling? C++ compilers haven&#x27;t done exceptions that way for decades. What happens if I longjmp or goto past a defer statement? This says it just doesn&#x27;t invoke the defer mechanism and may result in memory leaks or other damage. Does that mean it&#x27;s undefined behaviour? C++ won&#x27;t compile a goto past constructors for good reason.<p>All POSIX error and signal codes have an equivalent prefixed with DEFER_, e.g. DEFER_ENOMEM, DEFER_HUP. This is just in case the system doesn&#x27;t already have ENOMEM? Doesn&#x27;t the standard already require that ENOMEM exist? If not, why not just make this feature require that ENOMEM exist? Why depend so much on errno for new core language features when it&#x27;s basically an ugly artifact of ancient C library functions?<p>&gt; If C will be extended with lamdas (hopefully in a nearer future)<p>I wouldn&#x27;t hold my breath.