Dispatch Functions Depending on their Return Values

#include <functional>
#include <iostream>
#include <type_traits>
 
template<typename Task>
auto process(Task task)
-> typename std::enable_if_t<std::is_integral_v<decltype(task())>, void> {
    std::cout << "Processing task with 'integral' return value\n";
    //execute directly
}
 
template<typename Task
        ,typename = std::enable_if_t<std::is_void_v<decltype(std::declval<Task>()())>>
        >
void process(Task task){
    std::cout << "Processing task with 'void' return value\n";
    (void) task; // not used
    // queue task
}
 
 
int main() {
    process([]() -> int { return 1; });
    process([]() -> void {});
}

Posted in c++

Leave a Reply

Your email address will not be published. Required fields are marked *