我有几个模板函数的实例。它们中的每一个都按顺序执行每个给定的 lambda,并附带特定的消息。当我使用一个 lambda 执行此操作时,一切正常,但是当我尝试添加多个 lambda 时,我得到
note: candidate template ignored: deduced conflicting types for parameter 'Task'
Run Code Online (Sandbox Code Playgroud)
来自铿锵。这是我的代码:
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([&] …Run Code Online (Sandbox Code Playgroud) c++ templates function-templates template-argument-deduction c++17