Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core/assert: add magic_assert() and magic_static_assert()
`magic_static_assert()` abuses the optimizer and linker to provide the functionality of `static_assert()` for expressions that are formally nowhere near to constant integer expressions, but can still be fully evaluated at compile time. This can become a simple wrapper around `constexpr` and `static_assert()` once C gains support for `constexpr`. `magic_assert()` will do that same as `magic_static_assert()` if the argument can be evaluated at compile time, but will fall back to a standard `assert()` (hence, a run time check) where this is not possible. This is immensely useful when e.g. depending on the used board an expression can be evaluated at compile time or not. (E.g. some boards detect the CPU frequency at run-time, while most know it at compile time.) Unlike `assert()`, `magic_assert()` will break at compile-time when the assertion is known to always be false. Note: Unlike `assert(0)` a `magic_assert(0)` is never sensible. The former can be placed at code paths that must be unreachable, the latter will (unless the code path is eliminated as dead branch at compile time) fail to compile. Additionally, `magic_assert()` will not be fully disabled with `NDEBUG`, only the run-time checks will be disabled at compile time. As a result, expressions with side-effects as arguments `magic_assert()` will be still evaluated with `NDEBUG`.
- Loading branch information