diff --git a/include/gsl_assert.h b/include/gsl_assert.h index 51e8ab6..17a2a75 100644 --- a/include/gsl_assert.h +++ b/include/gsl_assert.h @@ -45,18 +45,31 @@ namespace gsl { -struct fail_fast : public std::runtime_error +struct fail_fast { - explicit fail_fast(char const* const message) : std::runtime_error(message) {} + virtual char const* what() const = 0; }; + +namespace details +{ + template + struct contract_violation : public E, public fail_fast + { + explicit contract_violation(char const* const message) : E(message) {} + virtual char const* what() const override + { + return E::what(); + } + }; +} } #if defined(GSL_THROW_ON_CONTRACT_VIOLATION) #define Expects(cond) if (!(cond)) \ - throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); + throw gsl::details::contract_violation("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); #define Ensures(cond) if (!(cond)) \ - throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); + throw gsl::details::contract_violation("GSL: Postcondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__)); #elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) diff --git a/tests/assertion_tests.cpp b/tests/assertion_tests.cpp index acd381a..cdcd2e0 100644 --- a/tests/assertion_tests.cpp +++ b/tests/assertion_tests.cpp @@ -31,6 +31,19 @@ SUITE(assertion_tests) { CHECK(f(2) == 2); CHECK_THROW(f(10), fail_fast); + try { f(10); } + catch (fail_fast const& e) { + cstring_span<> prefix = ensure_z("GSL: Precondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } + CHECK_THROW(f(10), std::logic_error); + try { f(10); } + catch (std::logic_error const& e) { + cstring_span<> prefix = ensure_z("GSL: Precondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } } int g(int i) @@ -44,7 +57,20 @@ SUITE(assertion_tests) { CHECK(g(2) == 3); CHECK_THROW(g(9), fail_fast); - } + try { g(9); } + catch (fail_fast const& e) { + cstring_span<> prefix = ensure_z("GSL: Postcondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } + CHECK_THROW(g(9), std::runtime_error); + try { g(9); } + catch (std::runtime_error const& e) { + cstring_span<> prefix = ensure_z("GSL: Postcondition failure"); + cstring_span<> what = ensure_z(e.what()).first(prefix.length()); + CHECK(prefix == what); + } + } } int main(int, const char *[])