Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/stlab/concurrency/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoExcept, R (*)(void*, Args...) noexcept, R (*)(void*, Args...)>;

private:
template <class F>
Expand Down
35 changes: 35 additions & 0 deletions test/task_contract_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@

#include <doctest/doctest.h>

TEST_CASE("noexcept task relocation callback preserves its signature") {
using task_t = stlab::task<int&(int&, int) noexcept>;
using callback_t = int& (*)(void*, int&, int) noexcept;
static_assert(std::is_same_v<task_t::invoke_t, callback_t>);
static_assert(
std::is_same_v<decltype(std::declval<const task_t&>().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<int&(int&, int)>;
using callback_t = int& (*)(void*, int&, int);
static_assert(std::is_same_v<task_t::invoke_t, callback_t>);
static_assert(
std::is_same_v<decltype(std::declval<const task_t&>().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<void() noexcept> first = [p = std::make_unique<int>(42), &observed]() noexcept {
Expand Down
Loading