Skip to content
This repository was archived by the owner on May 3, 2026. It is now read-only.

Commit 55f3583

Browse files
committed
fix Result::and_then for void specialization
1 parent 7889d5b commit 55f3583

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

include/common/result.hpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,13 @@ operator==(const Result<LhsT, LhsErrs...>& lhs, const Result<RhsT, RhsErrs...>&
363363
template<traits::IsResultError... Errs>
364364
requires(sizeof...(Errs) > 0)
365365
class Result<void, Errs...> {
366-
private:
367-
// helper type
368-
template<typename Self, typename F>
369-
using and_then_return_t = std::invoke_result_t<F, decltype((std::declval<Self>().m_value))>;
370-
371366
public:
367+
using value_type = void;
368+
using error_types = traits::type_pack<Errs...>;
369+
370+
// instead of wrapping the variant in std::optional, we can use std::monostate
371+
std::variant<std::monostate, Errs...> error; ///< Variant holding an error or monostate.
372+
372373
/**
373374
* @brief Construct a Result with an error.
374375
* @tparam E Error type (must be in Errs).
@@ -428,23 +429,19 @@ class Result<void, Errs...> {
428429
*/
429430
template<typename Self, typename F>
430431
constexpr auto and_then(this Self&& self, F&& f)
431-
requires std::invocable<F, decltype(std::forward<Self>(self).m_value)>
432-
&& traits::is_result_v<and_then_return_t<Self, F>>
433-
&& traits::contains_all_v<and_then_return_t<Self, F>, typename Self::error_types>
432+
requires std::invocable<F, void> && traits::is_result_v<std::invoke_result_t<F, void>>
433+
&& traits::
434+
contains_all_v<std::invoke_result_t<F, void>, typename Self::error_types>
434435
{
435436
// if there is an error, return said error immediately
436437
if (self.has_error()) {
437-
return std::visit([](auto&& var) -> and_then_return_t<Self, F> {
438+
return std::visit([](auto&& var) -> std::invoke_result_t<F, void> {
438439
return std::forward<decltype(var)>(var);
439440
}, std::forward<Self>(self));
440441
}
441442
// otherwise, invoke the callable and return the result
442443
return std::invoke(f, std::forward<Self>(self).m_value);
443444
}
444-
445-
private:
446-
// instead of wrapping the variant in std::optional, we can use std::monostate
447-
std::variant<std::monostate, Errs...> error; ///< Variant holding an error or monostate.
448445
};
449446

450447
} // namespace zest

0 commit comments

Comments
 (0)