Make future return reference instead of value

This commit is contained in:
Nikita Lisitsa 2025-09-06 15:35:07 +03:00
parent 0210b61540
commit 5c0d4d8d29

View file

@ -40,6 +40,18 @@ namespace psemek::async
using type = util::function<void()>; using type = util::function<void()>;
}; };
template <typename T>
struct future_result_type
{
using type = T &;
};
template <>
struct future_result_type<void>
{
using type = void;
};
struct cancel_token struct cancel_token
{}; {};
@ -139,7 +151,7 @@ namespace psemek::async
return has_value_unsafe(); return has_value_unsafe();
} }
T get() typename detail::future_result_type<T>::type get()
{ {
if (!state_) if (!state_)
throw empty_future_error{}; throw empty_future_error{};
@ -151,7 +163,7 @@ namespace psemek::async
if constexpr (std::is_same_v<T, void>) if constexpr (std::is_same_v<T, void>)
return; return;
else else
return std::move(*(state_->value)); return *(state_->value);
} }
else else
std::rethrow_exception(state_->exception); std::rethrow_exception(state_->exception);