diff --git a/include/stlab/concurrency/task.hpp b/include/stlab/concurrency/task.hpp index b7f7352..c3a5a9e 100644 --- a/include/stlab/concurrency/task.hpp +++ b/include/stlab/concurrency/task.hpp @@ -169,7 +169,8 @@ class task_ { using concept_t = stlab_v2_task_concept; /// Function used to invoke a task's target, valid for as long as the target is live. - using invoke_t = R (*)(void*, Args...) noexcept(NoExcept); + using invoke_t = + std::conditional_t; private: template diff --git a/test/task_contract_test.cpp b/test/task_contract_test.cpp index 5538a1b..7486254 100644 --- a/test/task_contract_test.cpp +++ b/test/task_contract_test.cpp @@ -15,6 +15,41 @@ #include +TEST_CASE("noexcept task relocation callback preserves its signature") { + using task_t = stlab::task; + using callback_t = int& (*)(void*, int&, int) noexcept; + static_assert(std::is_same_v); + static_assert( + std::is_same_v().relocation_invoke()), callback_t>); + + task_t target = [](int& value, int amount) noexcept -> int& { + value += amount; + return value; + }; + int value = 24; + CHECK(&target.relocation_invoke()(target.relocation_source(), value, 18) == &value); + CHECK(value == 42); +} + +TEST_CASE("throwing task relocation callback preserves its signature and propagates errors") { + using task_t = stlab::task; + using callback_t = int& (*)(void*, int&, int); + static_assert(std::is_same_v); + static_assert( + std::is_same_v().relocation_invoke()), callback_t>); + + task_t target = [](int& value, int amount) -> int& { + if (amount < 0) throw std::runtime_error("negative amount"); + value += amount; + return value; + }; + int value = 24; + CHECK(&target.relocation_invoke()(target.relocation_source(), value, 18) == &value); + CHECK(value == 42); + CHECK_THROWS_AS(target.relocation_invoke()(target.relocation_source(), value, -1), + std::runtime_error); +} + TEST_CASE("task retains a move-only capture through move construction") { int observed = 0; stlab::task first = [p = std::make_unique(42), &observed]() noexcept {