Skip to content

Commit

Permalink
Add bool conversion operator to Result class
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryKogan committed Dec 26, 2023
1 parent 35fc3d7 commit 26d8263
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ template <typename T, typename E> class Result {
[[nodiscard]] auto unwrap() const -> T;
[[nodiscard]] auto unwrap_err() const -> E;

/// @brief `true` if the Result is successful, `false` otherwise.
constexpr explicit operator bool() const noexcept { return is_ok(); }

/// @name Accesses the value of a successful Result.
/// @return Pointer or reference to the value.
///@{
Expand Down
2 changes: 2 additions & 0 deletions tests/is_ok.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ TEST(IsOk, Ok) {
const int value = 5;
res::Result<int, std::string> result = res::Ok(value);
EXPECT_TRUE(result.is_ok());
EXPECT_TRUE(result);
EXPECT_FALSE(result.is_err());
}

TEST(IsOk, Err) {
res::Result<int, std::string> result = res::Err(std::string("Something went wrong"));
EXPECT_FALSE(result.is_ok());
EXPECT_FALSE(result);
EXPECT_TRUE(result.is_err());
}

0 comments on commit 26d8263

Please sign in to comment.