nemequ / hedley

A C/C++ header to help move #ifdefs out of your code
https://nemequ.github.io/hedley/
Creative Commons Zero v1.0 Universal
774 stars 51 forks source link

Add a HEDLEY_ASSUME macro #16

Closed nemequ closed 5 years ago

nemequ commented 5 years ago

Given the discussion in #15, I think there is an appetite for a HEDLEY_ASSUME macro instead of just relying on HEDLEY_UNREACHABLE. Several compilers support a variant of assume (at least MSVC, clang, and TI. Probably ICC, too).

GCC is the big holdout, but I think something like (untested) this would work:

#define HEDLEY_ASSUME(expr) ((void) ((expr) ? 1 : (__builtin_unreachable(), 1)))

That would let us do this without relying on an if block.

nemequ commented 5 years ago

Just added a rough draft to dev branch.