Created
July 20, 2018 16:14
-
-
Save gatchamix/9059b1577a1d2f1c42eec01a653ab312 to your computer and use it in GitHub Desktop.
macro to automatically select 'static_assert' where possible, default to 'assert' otherwise
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 <cassert> | |
#define auto_assert(...) \ | |
[](auto expression) \ | |
{ \ | |
if constexpr (noexcept(expression())) \ | |
{ static_assert(expression()); } \ | |
else \ | |
{ assert(expression()); } \ | |
} \ | |
([&]{ return __VA_ARGS__; }) | |
int main(int argc, char **argv) | |
{ | |
auto_assert(1); // -> static_assert(1) | |
auto_assert(argc); // -> assert(argc) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment