3

我正在学习概念和模板。我正在尝试制作一个将调用另一个函数模板的函数模板。目前它适用于 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'”

我想做的事情可行吗?我也尝试使用模板模板参数,但似乎只适用于类型,不适用于函数。

4

1 回答 1

3

你的印象是

auto lambda = [](const auto& a){std::cout << a << " CALLED\n";};

相当于

template<typename T>
void lambda(const T& a) { std::cout << a << " CALLED\n"; }

但事实并非如此。它实际上相当于:

struct SomeType {
  template<typename T>
  void operator()(const T& a) const { std::cout << a << " CALLED\n"; }
};
SomeType lambda;

有趣的事实:如果您的 lambda 捕获值和/或引用,它们将成为该结构的私有成员,并且将 lambda 标记为可变只需删除const. Lambda 实际上只是函子之上的语法糖。

我想做的事情可行吗?

不幸的是,不符合当前标准。可以接受模板类型作为参数(通过一些看起来很时髦的语法),但函数模板目前仍然不在讨论范围内。

您几乎必须operator()像我的示例中那样使用模板声明结构或类,或者将它们包装在 lambda 中:

callFunc<int>([](auto const& a){testFuncToPass(a);});
于 2021-06-01T03:31:45.430 回答