2 ms·
Golang gets this wrong. It should be scope-level not function-level (or perhaps there should be two different types, but I have never personally had a need for
by rwmj 7y ago
Golang gets this wrong. It should be scope-level not function-level (or perhaps there should be two different types, but I have never personally had a need for a function-level cleanup).
Edit: Also please review how attribute cleanup is used by existing C code before jumping into proposals. If something is added to C2x which is inconsistent with what existing code is already doing widely, then it's no help to anyone.
- rseacord 7y agoYes, we have discussed adding this feature at scope level. A not entirely serious proposal was to implement it as follows: #define DEFER(a, b, c) \ for (bool _flag = true; _flag; _flag = false) \ for (a; _flag && (b); c, _flag = false) int fun() { DEFER(FILE *f1 = fopen(...), (NULL != f1), mfclose(f1)) { DEFER(FILE *f2 = fopen(...), (NULL != f2), mfclose(f2)) { DEFER(FILE *f3 = fopen(...), (NULL != f3), mfclose(f3)) { ... do something ... } } } } We are also looking at the attribute cleanup. Sounds like you should be involved in developing this proposal?
- rwmj 7y agoYes, I'll ask around in Red Hat too, see if we can get some help with this.
- a1369209993 7y agoApropos of this, I'll toss in: please support do-after statements (and also let statements). do foo(); _After bar(); /* exactly equivalent to (with gcc ({})s): */ ({ bar(); foo(); }); #define DEFER(a, b, c) \ _Let(a) if(!b) {} else do {c;} _After (This is in fact a entirely serious proposal, though I don't actually expect it to happen.)