Skip to content

Commit

Permalink
Move the error value when throwing an exception in .value rval overlo…
Browse files Browse the repository at this point in the history
…ads (#62)

* Move the error value when throwing an exception in .value rval overloads
Fixes #61

* Bump version
  • Loading branch information
TartanLlama authored Jul 10, 2019
1 parent bcdc78b commit 6fe2af5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions include/tl/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#define TL_EXPECTED_VERSION_MAJOR 1
#define TL_EXPECTED_VERSION_MINOR 0
#define TL_EXPECTED_VERSION_PATCH 0
#define TL_EXPECTED_VERSION_PATCH 1

#include <exception>
#include <functional>
Expand Down Expand Up @@ -1907,14 +1907,14 @@ class expected : private detail::expected_move_assign_base<T, E>,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
TL_EXPECTED_11_CONSTEXPR const U &&value() const && {
if (!has_value())
detail::throw_exception(bad_expected_access<E>(err().value()));
detail::throw_exception(bad_expected_access<E>(std::move(err()).value()));
return std::move(val());
}
template <class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
TL_EXPECTED_11_CONSTEXPR U &&value() && {
if (!has_value())
detail::throw_exception(bad_expected_access<E>(err().value()));
detail::throw_exception(bad_expected_access<E>(std::move(err()).value()));
return std::move(val());
}

Expand Down
11 changes: 10 additions & 1 deletion tests/issues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ TEST_CASE("Issue 49", "[issues.49]") {
auto m = test(10)
.and_then(test2);
}
#endif
#endif

tl::expected<int, std::unique_ptr<std::string>> func()
{
return 1;
}

TEST_CASE("Issue 61", "[issues.61]") {
REQUIRE(func().value() == 1);
}

0 comments on commit 6fe2af5

Please sign in to comment.