Skip to content

Instantly share code, notes, and snippets.

@jepler
Created June 27, 2024 01:11
Show Gist options
  • Save jepler/a344dd2be978f95a483e3a1fd2afbf73 to your computer and use it in GitHub Desktop.
Save jepler/a344dd2be978f95a483e3a1fd2afbf73 to your computer and use it in GitHub Desktop.
#include <stdexcept>
#include <cstdlib>
#define ASSERTION(pred) \
assertion_impl<__builtin_constant_p(pred)>()((pred))
int checks;
template<bool b>
struct assertion_impl {
void operator()(bool pred) { checks ++; if(!pred) abort(); }
};
template<>
struct assertion_impl<true> {
consteval void operator()(bool pred) { (void)(pred ? 0 : throw std::logic_error("static assertion failure")); }
};
#include <stdio.h>
int main(int argc, char **argv) {
ASSERTION(true);
ASSERTION(argc > 1);
#if FAIL
ASSERTION(false);
#endif
printf("checks=%d\n", checks);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment