diff --git a/result.h b/result.h index 32f76c5..2a1d9f1 100644 --- a/result.h +++ b/result.h @@ -33,12 +33,15 @@ template class err { explicit err(E error) : error_(std::move(error)) {} template operator result() const; // NOLINT(google-explicit-constructor) + operator result() const; // NOLINT(google-explicit-constructor) }; template template inline err::operator result() const { return result(error_, false); } +template inline err::operator result() const { return result(error_); } + } // namespace res #include @@ -109,7 +112,7 @@ template class result { E error_; explicit result() : successful_(true) {} - explicit result(const E &error) : error_(error), successful_(false) {} + explicit result(E error) : error_(std::move(error)), successful_(false) {} template friend class ok; template friend class err; @@ -123,12 +126,17 @@ template class result { }; template inline auto result::value() const -> const T & { - if (!is_ok()) { throw std::runtime_error("value() called on result with error"); } + if (!is_ok()) { throw std::logic_error("value() called on result with error"); } return value_; } template inline auto result::error() const -> const E & { - if (is_ok()) { throw std::runtime_error("error() called on result with value"); } + if (is_ok()) { throw std::logic_error("error() called on result with value"); } + return error_; +} + +template inline auto result::error() const -> const E & { + if (is_ok()) { throw std::logic_error("error() called on result with value"); } return error_; } diff --git a/src/err/err.hpp b/src/err/err.hpp index 4cfe739..cfde67d 100644 --- a/src/err/err.hpp +++ b/src/err/err.hpp @@ -16,10 +16,13 @@ template class err { explicit err(E error) : error_(std::move(error)) {} template operator result() const; // NOLINT(google-explicit-constructor) + operator result() const; // NOLINT(google-explicit-constructor) }; template template inline err::operator result() const { return result(error_, false); } +template inline err::operator result() const { return result(error_); } + } // namespace res \ No newline at end of file diff --git a/src/result/result.hpp b/src/result/result.hpp index 0ac3d50..de73112 100644 --- a/src/result/result.hpp +++ b/src/result/result.hpp @@ -45,7 +45,7 @@ template class result { E error_; explicit result() : successful_(true) {} - explicit result(const E &error) : error_(error), successful_(false) {} + explicit result(E error) : error_(std::move(error)), successful_(false) {} template friend class ok; template friend class err; @@ -59,12 +59,17 @@ template class result { }; template inline auto result::value() const -> const T & { - if (!is_ok()) { throw std::runtime_error("value() called on result with error"); } + if (!is_ok()) { throw std::logic_error("value() called on result with error"); } return value_; } template inline auto result::error() const -> const E & { - if (is_ok()) { throw std::runtime_error("error() called on result with value"); } + if (is_ok()) { throw std::logic_error("error() called on result with value"); } + return error_; +} + +template inline auto result::error() const -> const E & { + if (is_ok()) { throw std::logic_error("error() called on result with value"); } return error_; } diff --git a/tests/boolean_operations.cpp b/tests/boolean_operations.cpp index a6ec69a..abf6191 100644 --- a/tests/boolean_operations.cpp +++ b/tests/boolean_operations.cpp @@ -10,6 +10,7 @@ TEST(BooleanOperations, Ok) { EXPECT_TRUE(result.is_ok()); EXPECT_FALSE(!result.is_ok()); EXPECT_EQ(result.value(), 42); + EXPECT_THROW(auto val = result.error(), std::logic_error); // NOLINT } TEST(BooleanOperations, Err) { @@ -20,6 +21,7 @@ TEST(BooleanOperations, Err) { EXPECT_FALSE(result.is_ok()); EXPECT_TRUE(!result.is_ok()); EXPECT_EQ(result.error(), "error"); + EXPECT_THROW(auto val = result.value(), std::logic_error); // NOLINT } TEST(BooleanOperations, VoidOk) { @@ -28,4 +30,16 @@ TEST(BooleanOperations, VoidOk) { EXPECT_FALSE(!result); EXPECT_TRUE(result.is_ok()); EXPECT_FALSE(!result.is_ok()); + EXPECT_THROW(auto val = result.error(), std::logic_error); // NOLINT +} + +TEST(BooleanOperations, VoidErr) { + const std::string error = "error"; + res::result result = res::err(error); + EXPECT_FALSE(result); + EXPECT_TRUE(!result); + EXPECT_FALSE(result.is_ok()); + EXPECT_TRUE(!result.is_ok()); + EXPECT_EQ(result.error(), "error"); + // EXPECT_THROW(auto val = result.value(), std::logic_error); - Should not compile } \ No newline at end of file