std :: function的说明 [英] Explanation of std::function

查看:87
本文介绍了std :: function的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: function 的目的是什么?据我了解, std :: function 将函数,函子或lambda转换为函数对象。



我不太了解这个目的……Lambda和Functors都已经是函数对象,我相信它们可以用作排序和转换等算法的谓词。作为附带说明,Lambda实际上是(内部)函子。因此,我只能看到 std :: function 有用的是将常规函数转换为函数对象。



而且我不太清楚为什么我也想将常规函数转换为函数对象。如果我想使用一个函数对象,那么我首先会做一个函子或lambda ...,而不是编写一个函数,然后用std :: function对其进行转换,然后将其作为谓词传递给<... / p>

我猜想 std :: function 还有更多...乍看之下很明显。



std :: function 的解释将不胜感激。

解决方案


std :: function ?据我了解, std :: function 将函数,函子或lambda转换为函数对象。


std :: function 是一个更广泛的概念(称为类型擦除)的示例。您的描述不太准确。 std :: function< void()> 所做的选择特定的专业表示 any 可调用,无需参数即可调用。它可以是具有具体类型的函数指针或函数对象,也可以是从lambda构建的闭包。只要符合合同,源类型是什么都可以,它就可以工作。而不是使用具体的源类型,我们擦除它-我们只处理 std :: function



<现在,为什么我们要使用类型擦除?毕竟,我们没有模板以便可以直接使用具体类型吗?那不是更有效吗,不是C ++就是效率吗?!



有时,您不能使用具体类型。一个可能更熟悉的示例是常规的面向对象的多态性。为什么当我们可以存储 Derived * 时,为什么还要存储 Base * ?好吧,也许我们不能存储 Derived * 。也许我们有许多不同的 Derived * 供不同的用户使用。也许我们正在编写一个甚至都不知道派生的库。这也是 类型擦除,只是一种与 std :: function 使用的技术不同的擦除方法。



用例的非穷尽列表:




  • 需要存储当我们只关心满足特定接口的对象时,它们可能是异构的对象列表。对于 std :: function ,也许我只有一个 std :: vector< std :: function< void()>>回调-可能都有不同的具体类型,但我不在乎,我只需要调用它们即可。

  • 需要跨API边界使用(例如,我可以使用 virtual 函数并使用 std :: function< void()> ,但是我可以t有一个虚拟函数模板)。

  • 从工厂函数返回-我们只需要一些满足某些概念的对象,我们不需要不需要具体的事情(同样,在OO多态中很常见,这也是类型擦除)。

  • 可能实际上在任何地方都使用模板,但是性能提升不值得编译击中。


What is the purpose of std::function? As far as I understand, std::function turns a function, functor, or lambda into a function object.

I don't quite understand the purpose of this... Both Lambdas and Functors are function objects already and I do believe that they can be used as predicates for algorithms like sort and transform. As a side note, Lambdas are actually Functors (internally). So the only thing I can see std::function being useful for is to turn regular functions into function objects.

And I don't quite see why I would want to turn a regular function into a function object either. If I wanted to use a function object I would have made one in the first place as a functor or lambda... rather than code a function and then convert it with std::function and then pass it in as predicate...

I'm guessing that there is much more to std::function... something that isn't quite obvious at first glance.

An explanation of std::function would be much appreciated.

解决方案

What is the purpose of std::function? As far as I understand, std::function turns a function, functor, or lambda into a function object.

std::function is an example of a broader concept called Type Erasure. The description you have isn't quite accurate. What std::function<void()> does, to pick a specific specialization, is represent any callable that can be invoked with no arguments. It could be a function pointer or a function object that has a concrete type, or a closure built from a lambda. It doesn't matter what the source type is, as long as it fits the contract - it just works. Instead of using the concrete source type, we "erase" it - and we just deal with std::function.

Now, why would we ever use type erasure? After all, don't we have templates so that we can use the concrete types directly? And wouldn't that be more efficient and isn't C++ all about efficiency?!

Sometimes, you cannot use the concrete types. An example that might be more familiar is regular object-oriented polymorphism. Why would we ever store a Base* when we could instead store a Derived*? Well, maybe we can't store a Derived*. Maybe we have lots of different Derived*s that different users use. Maybe we're writing a library that doesn't even know about Derived. This is also type erasure, just a different technique for it than the one std::function uses.

A non-exhaust list of use-cases:

  • Need to store a potentially heterogenous list of objects, when we only care about them satisfying a concrete interface. For std::function, maybe I just have a std::vector<std::function<void()>> callbacks - which might all have different concrete types, but I don't care, I just need to call them.
  • Need to use across an API boundary (e.g. I can have a virtual function taking a std::function<void()>, but I can't have a virtual function template).
  • Returning from a factory function - we just need some object that satisfies some concept, we don't need a concrete thing (again, quite common in OO polymorphism, which is also type erasure).
  • Could potentially actually use templates everywhere, but the performance gain isn't worth the compilation hit.

这篇关于std :: function的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆