#include namespace experimental { // Peter's constexpr enhanced error category class error_category { public: constexpr error_category() = default; virtual std::string message(int c) const = 0; // NEW: True if the code is considered a failure virtual bool failure(int code) const noexcept { // Defaults to comparing to 0 for backwards compatibility with existing code return 0 != code; } }; // Peter's constexpr enhanced error code class error_code { int _value; const error_category *_category; public: constexpr error_code(int v, const error_category &cat) : _value(v), _category(&cat) {} constexpr int value() const noexcept { return _value; } constexpr const error_category &category() const noexcept { return *_category; } std::string message() const { return _category->message(_value); } // Fixed operator bool, now asks category explicit operator bool() const noexcept { return _category->failure(_value); } }; } namespace experimental2 { class error_code { int _value; bool _is_failure; const experimental::error_category *_category; public: constexpr error_code(int v, const experimental::error_category &cat) : _value(v), _is_failure(v != 0), _category(&cat) {} constexpr error_code(int v, bool is_failure, const experimental::error_category &cat) : _value(v), _is_failure(is_failure), _category(&cat) {} constexpr int value() const noexcept { return _value; } constexpr const experimental::error_category &category() const noexcept { return *_category; } std::string message() const { return _category->message(_value); } // Fixed operator bool, now asks category explicit operator bool() const noexcept { return _is_failure; } }; } // Test error code scheme where 0 is a failure, non-zero a success enum class code { failure=0, success=1 }; extern const experimental::error_category& code_category;