Skip to content

Instantly share code, notes, and snippets.

@gatchamix
Created July 20, 2018 16:14
Show Gist options
  • Save gatchamix/9059b1577a1d2f1c42eec01a653ab312 to your computer and use it in GitHub Desktop.
Save gatchamix/9059b1577a1d2f1c42eec01a653ab312 to your computer and use it in GitHub Desktop.
macro to automatically select 'static_assert' where possible, default to 'assert' otherwise
#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