Skip to content

Instantly share code, notes, and snippets.

@socantre
Created June 1, 2017 19:24
Show Gist options
  • Save socantre/c8e69c4c7232d822b2711f13b46e969b to your computer and use it in GitHub Desktop.
Save socantre/c8e69c4c7232d822b2711f13b46e969b to your computer and use it in GitHub Desktop.
require() with REQUIRE() macro to deal with source locations, until we get a real std::source_location type
#include <stdexcept>
struct require_error : std::runtime_error {
using std::runtime_error::runtime_error;
};
void require(bool b, char const *message) {
if (!b) {
throw require_error(message);
}
}
#define STRINGIZE2(x) STRINGIZE(x)
#define STRINGIZE(x) #x
#define SOURCE_LOCATION __FILE__ ":" STRINGIZE2(__LINE__)
#define REQUIRE(condition, reason) (require((condition), SOURCE_LOCATION ": " reason))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment