Is it legal to use side-effects in exceptions thrown by constexpr?
Normally, constexpr must be free of side-effects. However, I just discovered that it is possible to use side-effects in the constructors of thrown exceptions. That technique can be used to emulate assert() for constexpr functions, as it is demonstrated in the following program. #include <iostream> #include <cstdlib> #include <stdexcept> struct constexpr_precond_violated : std::logic_error { constexpr_precond_violated(const char* msg) : std::logic_error(msg) { std::cerr << msg << '\n'; abort(); // to get a core dump } }; #define TO_STRING_IMPL(x) #x #define TO_STRING(x) TO_STRING_IMPL(x)