diff options
author | Håvard Pettersen <havardpe@yahooinc.com> | 2022-10-13 11:27:00 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@yahooinc.com> | 2022-10-13 11:27:00 +0000 |
commit | 7e4b77ee4e02b70e92908ba3bd4af5e0a85a48f5 (patch) | |
tree | abd31f608281181cf8c4fab4eb2682d2e4638342 /vespalib | |
parent | 045064168f4d9209d2bb5a5aa8a176300edf31f5 (diff) |
unify return value handling using universal references
the requires clause is there to make sure we are only allowed to
return values that are implicitly convertible to T; this will avoid
calling explicit constructors 'implicitly' (as seem from the co_return
statement).
(the internal optional requires assignability to also cover the case
where the value is not constructed, but overwritten, which probably
imposes a similar restriction, but if we stop using optional, the
require becomes more useful)
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/coro/lazy.h | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/vespalib/src/vespa/vespalib/coro/lazy.h b/vespalib/src/vespa/vespalib/coro/lazy.h index 38a57108f04..5a10c05bc24 100644 --- a/vespalib/src/vespa/vespalib/coro/lazy.h +++ b/vespalib/src/vespa/vespalib/coro/lazy.h @@ -38,11 +38,10 @@ public: }; return awaiter(); } - void return_value(const T &ret_value) noexcept(std::is_nothrow_move_constructible_v<T>) requires(std::copy_constructible<T>) { - value = ret_value; - } - void return_value(T &&ret_value) noexcept(std::is_nothrow_move_constructible_v<T>) { - value = std::move(ret_value); + template <typename RET> + requires std::is_convertible_v<RET&&,T> + void return_value(RET &&ret_value) noexcept(std::is_nothrow_constructible_v<T,RET&&>) { + value = std::forward<RET>(ret_value); } void unhandled_exception() noexcept { exception = std::current_exception(); @@ -52,7 +51,7 @@ public: std::coroutine_handle<> waiter; promise_type(promise_type &&) = delete; promise_type(const promise_type &) = delete; - promise_type() : value(std::nullopt), exception(), waiter(std::noop_coroutine()) {} + promise_type() noexcept : value(std::nullopt), exception(), waiter(std::noop_coroutine()) {} T &result() & { if (exception) { std::rethrow_exception(exception); |