<分区>
我确定错误非常简单和愚蠢,但我看不到。这是代码:
#include <future>
template <typename ResultType>
class Foo
{
public:
template <typename ...Args>
void exec(const std::function<ResultType(Args...)>& task, Args&&... args) {}
};
int main()
{
Foo<void>().exec([](){});
return 0;
}
这里是错误:
'void CAsyncTask::exec(const std::function &,Args &&...)' : could not deduce template argument for 'const std::function &' with [ ResultType=void ]
Foo<void>().exec<void>([](){})
也不起作用(我更愿意不必手动指定 Args
类型)。
关于建议答案的更新:以下代码确实有效。
CAsyncTask<void>().exec(std::function<void ()>([](){}));
但是这个问题真的没有解决方法吗?我能否以某种方式扩展我的模板以推断出 lambda 参数?