3 ms·
> Can assert() actually function this way? Can you use assert to tell the optimizer it can assume something is true? Trivially. #ifdef NDEBUG # define as
by quotemstr 11y ago
> Can assert() actually function this way? Can you use assert to tell the optimizer it can assume something is true?
Trivially.
#ifdef NDEBUG
# define assert_and_assume(cond) if(!(cond))
__builtin_unreachable((cond))
#else
# define assert_and_assume assert
#endif
- haberman 11y agoInteresting! This assert_and_assume() seems strictly better than vanilla assert() for any predicates that don't have side effects. But I guess you have to be sure that the compiler is able to deduce that there aren't side effects and feels comfortable optimizing away the predicate in release mode.