Created
June 27, 2024 01:11
-
-
Save jepler/a344dd2be978f95a483e3a1fd2afbf73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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