This is an example of taking something that should be multiple statements, and condensing it into a difficult-to-understand single statement using a couple of language rules.
The code is equivalent to this:
#define except(expression, message) \
if (!!(expression)) {} \
else { \
throw std::runtime_error(message); \
}
The ||
takes advantage of short-circuiting to evaluate the RHS only if the LHS evaluates to false
.
The !!
is a trick to help ensure that the result is a boolean, or at least boolean-like. Traditionally, some user-defined types do not convert automatically to bool
, but do provide a operator!
; applying that for a second time undoes the natural negation that's implied by operator!
. In the case of built-in types (e.g. integer types) it may be considered as nothing but a (pointless) explicit conversion to bool
.
The , false
takes advantage of the comma operator's propensity to cause the resulting expression to have the type of its RHS-most operand. You want the type of the expression to be bool
so that it can be applied to the ||
operator, and a throw
-expression has type void
, so the , false
corrects for that.
The cast (void)
ensures that you cannot accidentally use the meaningless result of the expression as a value.
Try not to write code like this.