I have few instances of one template function. Each of them sequentially executes each of given lambdas, accompanying them with specific messages. When I do that with one lambda, everything works fine, but when I try to add more than one, I get
note: candidate template ignored: deduced conflicting types for parameter 'Task'
from Clang. Here is my code:
template <class Task>
void doTasks(Task task1) { //works fine
if (std::__is_invocable<Task>::value) {
std::cout << "doing first task" << endl;
task1;
}
}
template <class Task>
void doTasks(Task task1, Task task2) { //deduced conflicting types
if (std::__is_invocable<Task>::value) {
std::cout << "doing first task" << endl;
task1();
std::cout << "doing second task" << endl;
task2();
}
}
int main(){
doTasks([&] (){std::cout << "1" << endl;});
doTasks([&] (){std::cout << "1" << endl;},
[&] (){std::cout << "2" << endl;});
return 0;
}
What’s wrong with it? How can I deal with my problem?
Sorry if it’s a stupid question, I’m some kind of a beginner in C++ and may not understand some template nuances.
>Solution :
The lambdas are of different types. You need to add another template parameter for the second one:
#include <type_traits> // for the proper `std::invocable`
template <class Task, class AnotherTask>
void doTasks(Task task1, AnotherTask task2) {
if constexpr (std::is_invocable_v<Task> && std::is_invocable_v<AnotherTask>) {
std::cout << "doing first task\n";
task1();
std::cout << "doing second task\n";
task2();
}
}
Note: Since it’s C++17, you can also use constexpr if to deal with the if at compile time only.