As suggested by sipa in the open issue #4576 (2014) – “[discussion] Dealing with assertions and optional consistency checking” – as an alternative to assert(…)
in situations where assert(…)
is not appropriate:
… What I want is more of these checks, more as a way for the programmer to say “this is what I assume here”, more than “if this doesn’t hold here, we’re in BIG trouble”. It makes the code clearer, and simultaneously verifies that such assumptions hold. But only in cases where we’re not at risk of hurting the network by dying. …
ASSUME(expression)
works like this:
- If compiled with
-DABORT_ON_FAILED_ASSUME
(set by--enable-debug
and/or--enable-fuzz
): Evaluate expression and abort ifexpression
isfalse
. - If compiled without
-DABORT_ON_FAILED_ASSUME
: Evaluateexpression
and continue execution.
Example:
0int main(void) {
1 ASSUME(IsFoo());
2 ...
3}
If !IsFoo()
and -DABORT_ON_FAILED_ASSUME
, then:
0 filename.cpp:123: main: ASSUME(IsFoo()) failed.
1 Aborted
Otherwise the execution continues.
Resolves #4576.