| Taskflow: A General-purpose Task-parallel Programming System

Loading...
Searching...
No Matches
tf::ConditionTaskLike Concept Reference

concept to check if a callable is a condition task More...

`#include <taskflow/core/task.hpp>`

## Concept definition

template<typename C>
concept tf::ConditionTaskLike = std::invocable<C> &&
std::convertible_to<std::invoke_result_t<C>, int>
tf::ConditionTaskLike
concept to check if a callable is a condition task
Definition task.hpp:397

## Detailed Description

concept to check if a callable is a condition task

A condition task is a callable object that takes no arguments and returns a value convertible to int. The returned int value controls which successor branch is taken in conditional execution (if-else branching).

Requirements
  - Must be invocable with no arguments
  - Must return a value convertible to int

Examples

// Valid condition tasks (satisfy ConditionTaskLike)
// Simple condition returning int
auto condition_task1 = []() { return 0; };
static_assert(ConditionTaskLike<decltype(condition_task1)>);
// Condition with captured state
int counter = 0;
auto condition_task2 = [counter]() {
return (counter > 5) ? 1 : 0; // returns 1 (true) or 0 (false)
};
static_assert(ConditionTaskLike<decltype(condition_task2)>);
// Condition that evaluates runtime state
auto evaluate_path = []() {
// Return branch index (0 for if, 1 for else, etc.)
return rand() % 3;
};
static_assert(ConditionTaskLike<decltype(evaluate_path)>);
// Function object
struct ConditionTask {
int operator()() const { return 42; }
};
static_assert(ConditionTaskLike<ConditionTask>);
// Returns bool (convertible to int)
auto bool_condition = []() { return true; };
static_assert(ConditionTaskLike<decltype(bool_condition)>);
// Use in taskflow
tf::Taskflow taskflow;
auto [init, cond, branch_a, branch_b] = taskflow.emplace(
[](){ std::cout << "init\n"; },
condition_task1,
[](){ std::cout << "branch a\n"; },
[](){ std::cout << "branch b\n"; }
);
init.precede(cond);
cond.precede(branch_a, branch_b); // branch_a if cond returns 0, branch_b if returns 1
tf::FlowBuilder::emplace
Task emplace(C &&callable)
creates a static task
Definition flow_builder.hpp:1738
tf::Task::precede
Task & precede(Ts &&... tasks)
adds precedence links from this to other tasks
Definition task.hpp:1305
tf::Taskflow
class to create a taskflow object
Definition taskflow.hpp:64

Invalid Examples

// Invalid: takes parameters
auto not_condition1 = [](int x) { return x; };
// static_assert(ConditionTaskLike<decltype(not_condition1)>); // FAILS
// Invalid: returns non-convertible type
auto not_condition2 = []() { return std::string("hello"); };
// static_assert(ConditionTaskLike<decltype(not_condition2)>); // FAILS
// Invalid: returns void
auto not_condition3 = []() { std::cout << "task\n"; };
// static_assert(ConditionTaskLike<decltype(not_condition3)>); // FAILS