Skip to content

Instantly share code, notes, and snippets.

@hryniuk
Created April 15, 2017 09:14
Show Gist options
  • Save hryniuk/8d0c824c9dd21b4fec9dd81b59668aad to your computer and use it in GitHub Desktop.
Save hryniuk/8d0c824c9dd21b4fec9dd81b59668aad to your computer and use it in GitHub Desktop.
Ensure macro to check preconditions
#include <iostream>
#define ENSURE(x) do { \
if (not (x)) {std::cerr << __FILE__ << " " << __PRETTY_FUNCTION__ << ":" << __LINE__ << " " << #x << " assert failed!\n";} \
} while(false)
void f(int x)
{
ENSURE(x > 0);
}
int main()
{
f(10);
f(-1); // print "ensure.cpp void f(int):9 x > 0 assert failed"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment