diff --git a/include/errors/error.hpp b/include/errors/error.hpp index c664762..b1212fa 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -18,12 +18,6 @@ class Error { Error(const std::shared_ptr& message_ptr); public: - - /** - * @brief Constructs an empty error object. - */ - Error(); - /** * @brief Returns the error message. * @@ -54,6 +48,7 @@ class Error { explicit operator bool() const; friend Error make(const std::string& msg); + friend const Error& nil(); /** * @brief Writes the string representation of an error object to the given @@ -83,4 +78,11 @@ class Error { */ Error make(const std::string& msg); + +/** + * @brief Gets a constant reference of an empty error. + * @return A constant reference of an empty error. + */ +const Error& nil(); + } // namespace error diff --git a/src/error.cpp b/src/error.cpp index 40d756f..f702810 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -4,8 +4,6 @@ namespace errors { Error::Error(const std::shared_ptr& message_ptr) : message_ptr(message_ptr) {} -Error::Error() {} - std::string_view Error::message() const { if (!message_ptr) return "no error"; return *message_ptr; @@ -23,4 +21,9 @@ Error make(const std::string& msg) { return Error(std::make_shared(msg)); } +const Error& nil() { + static const Error err(nullptr); + return err; +} + } // namespace error diff --git a/test/error_test.cpp b/test/error_test.cpp index 6b8b1a4..c544fc3 100644 --- a/test/error_test.cpp +++ b/test/error_test.cpp @@ -9,7 +9,7 @@ TEST_CASE("Error Construction") { } TEST_CASE("Empty Error Construction") { - const errors::Error err; + const auto err = errors::nil(); REQUIRE_FALSE(err); REQUIRE(err.message() == "no error"); }