我正在学习概念和模板。我正在尝试制作一个将调用另一个函数模板的函数模板。目前它适用于 lambda,但不适用于普通功能。
// This also works with lambda but not the normal function:
//void callFunc(std::regular_invocable<T> auto invocable)
template<typename T>
void callFunc(auto invocable)
{
invocable(T(5));
}
// Doesn't work, whether I use "T" or "auto"
//template<typename T>
void testFuncToPass(const auto& a)
{
std::cout << a << " CALLED\n";
}
//...
auto lambda = [](const auto& a){std::cout << a << " CALLED\n";};
//callFunc<int>(testFuncToPass); // ERROR
callFunc<int>([](const auto& a){std::cout << a << " CALLED\n";});
callFunc<int>(lambda);
它说:“错误:没有匹配函数调用'callFunc'。候选模板被忽略:无法推断模板参数'invocable:auto'”
我想做的事情可行吗?我也尝试使用模板模板参数,但似乎只适用于类型,不适用于函数。