#define DEFER(EXPR) for(int _tmp=1; _tmp; _tmp=0,(EXPR))
Example: char * data = malloc(32);
DEFER(free(data))
{
// do stuff
}Most of the challenge in writing code with a single return seems to come from the fact that I'm not used to doing it. Of course, sometimes it leads to heavily nested control flow, although I'm not sure yet whether or not I mind that. It does certainly simplify reasoning about control flow though, especially in large functions.
That all being said, I'd still rather just have defer.
For instance parameter validation is one instance where I think an early return is very much warranted. There you typically have no cleanup to do since it's effectively the prelude of the function.
Regarding nested error handling, I prefer the "goto fail" pattern. Some people are opposed to the use of goto as a matter of principle but I think in this case it's fine. It's effectively the poor man's RAII when you don't have destructors.
That's what the assert statement is for; checking pre- and postconditions.
Alternatively you might be able to use nested functions to guard against a stray return but that's not standard.
static unsigned int __deferDepth = 0;
#define RETURN return
#define return ((__deferDepth == 0) ? RETURN : break do_return)
#define DEFER(EXPR) \
__label__ do_return; \
for(int _tmp = 1; _tmp; _tmp = (do { \
__deferDepth += 1; \
EXPR; \
__deferDepth -= 1; \
break; \
do_return: \
RETURN; \
}while(0);))
Nested DEFERs could get hairy, though (do you need a stack of __returnNotBreak values?), and there probably are issues if EXPR contains some nested loops with break or return.Regardless, this isn’t C. Statement expressions (and __label_) are a gcc* extension, and if you’re willing to go there, __attribute__((cleanup)) seems the wiser route.
But the really classic way of doing "defer" in C is to do what Linux does:
int foo(void)
{
char *ptr = malloc(13);
int error = 0;
error = bar();
if (error < 0)
goto out_free;
// do something else
out_free:
free(ptr);
return error;
}
Once you get used to it, it feels very natural to write and understand.[0] https://web.archive.org/web/20180426195701/http://fdiv.net/2...
The magic is basically:
static inline void defer_cleanup(void (^*b)(void)) { (*b)(); }
#define defer_merge(a,b) a##b
#define defer_varname(a) defer_merge(defer_scopevar_, a)
#define defer __attribute__((cleanup(defer_cleanup))) void (^defer_varname(__COUNTER__))(void) =
Which let's you do: FILE *a = fopen ("a.txt", "r");
if (!a)
return EXIT_FAILURE;
defer
{
fclose (a);
};
It uses cleanup and blocks, both of which are extensions. There may also be some strangeness with the way blocks hold memory. (Block references become const copies).The original link works.
Regarding its use in the wild, I've encountered it exactly once, in the lastpass-cli (available on github). I personally wouldn't consider using it for one of my projects because it's too nonstandard, however I suppose that for a security-sensitive application it might reduce the likelihood of messing error handling and leaking things all over the place. That being said using non-standard features might also make it more likely for a contributor to misunderstand what the code does precisely and introduce a problem.
You can "emulate" the defer statement with a Scope Guard[0], or if you're willing to use a macro, you can use the __COUNTER__ macro as an "anonymous" variable.
The implementation is very simple, it's just using a class for its destructor:
template <class F>
class final_act
{
public:
explicit final_act(F f) noexcept : f_(std::move(f)), invoke_(true) {}
final_act(final_act&& other) noexcept : f_(std::move(other.f_)), invoke_(other.invoke_)
{
other.invoke_ = false;
}
final_act(const final_act&) = delete;
final_act& operator=(const final_act&) = delete;
~final_act() noexcept
{
if (invoke_) f_();
}
private: F f_;
bool invoke_;
};Source: https://github.com/microsoft/GSL/blob/ebe7ebfd855a95eb937831...
Yep! I really like the defer pattern for those situations :)