高阶功能的功能签名
定义将函数作为参数的函数时,按值和通用参考进行函数参数有什么区别? (即):
template <typename Fn,typename... Args>
void apply(Fn&& fn, Args &&... args);
或者
template <typename Fn,typename... Args>
void apply(Fn fn, Args &&... args);
也许问题是:函数模板参数是否应该是最大化通用性的通用引用?或举例说明两个签名都会产生不同的行为。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只通过功能,我相信几乎没有区别。在这两种情况下,功能都将首先转换为功能指针。
如果要通过a std :: function 一样,情况会变得不同。如果按值呼叫,将有一个副本。有时也许这不是您的期望。
例如
,我相信这确实取决于您的情况。
也许您可以看一下STL的签名,看来他们倾向于进行普遍参考。
例如
std :: bind
或约束
std :: thread :: thread
If you only pass functions, I believe there's little difference. In both cases, function will be first converted to function pointers.
If you're to pass a FunctionObject, like a lambda or
std::function
, things will become different. There'll be a copy if call by value. Sometimes maybe it's not what you expect.e.g.
But it really depends on your situation, I believe.
Maybe you can take a look at the signiture in STL, it seems they tend to take a universal reference.
e.g.
std::bind
or construtor of
std::thread
关于参数类型的大多数一般考虑也适用于传递可可。我将您推荐给您的核心准则。
考虑
operator()
可以基于l/r-valueness超载::
Most general considerations for type of arguments apply to passing callables as well. I refer you to the core guidelines https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#fcall-parameter-passing.
Consider that
operator()
can be overloaded based on l/r-valueness:Output: