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

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

determines if a callable is a subflow task More...

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

## Concept definition

template<typename C>
concept tf::SubflowTaskLike = std::invocable<C, tf::Subflow&> &&
std::same_as<std::invoke_result_t<C, tf::Subflow&>, void>
tf::SubflowTaskLike
determines if a callable is a subflow task
Definition task.hpp:220

## Detailed Description

determines if a callable is a subflow task

A subflow task is a callable object that takes a tf::Subflow& reference and returns void. Subflow tasks allow dynamic creation of nested tasks at runtime. It is constructible from std::function<void(tf::Subflow&)>.

Requirements
  - Must be invocable with a tf::Subflow& argument
  - Must return void

Examples

// Valid subflow tasks (satisfy SubflowTaskLike)
// Basic subflow task
auto subflow_task = [](tf::Subflow& sf) {
auto task1 = sf.emplace([]() { std::cout << "subtask 1\n"; });
auto task2 = sf.emplace([]() { std::cout << "subtask 2\n"; });
task1.precede(task2);
};
static_assert(SubflowTaskLike<decltype(subflow_task)>);
// Subflow with captured context
int num_tasks = 5;
auto dynamic_subflow = [num_tasks](tf::Subflow& sf) {
for(int i = 0; i < num_tasks; ++i) {
sf.emplace([i]() { std::cout << "task " << i << '\n'; });
}
};
static_assert(SubflowTaskLike<decltype(dynamic_subflow)>);
// Function object
struct SubflowTask {
void operator()(tf::Subflow& sf) const {
sf.emplace([]() { std::cout << "nested\n"; });
}
};
static_assert(SubflowTaskLike<SubflowTask>);
// Use in taskflow
tf::Taskflow taskflow;
auto task = taskflow.emplace(subflow_task).name("subflow");
tf::FlowBuilder::emplace
Task emplace(C &&callable)
creates a static task
Definition flow_builder.hpp:1571
tf::Subflow
class to construct a subflow graph from the execution of a dynamic task
Definition flow_builder.hpp:1735
tf::Task::name
const std::string & name() const
queries the name of the task
Definition task.hpp:1388
tf::Taskflow
class to create a taskflow object
Definition taskflow.hpp:64

Invalid Examples

// Invalid: takes no parameters
auto not_subflow1 = []() { std::cout << "no params\n"; };
// static_assert(SubflowTaskLike<decltype(not_subflow1)>); // FAILS
// Invalid: takes non-Subflow parameter
auto not_subflow2 = [](int x) { std::cout << x << '\n'; };
// static_assert(SubflowTaskLike<decltype(not_subflow2)>); // FAILS
// Invalid: returns non-void
auto not_subflow3 = [](tf::Subflow& sf) { return 42; };
// static_assert(SubflowTaskLike<decltype(not_subflow3)>); // FAILS