Skip to content

std::function 类型的功能的实现

在下面的文章中,提供了类似的实现:

1、sean-parent Polymorphic Task Template in Ten

2、groups.google Safe 'type erasure' without runtime memory allocation

3、sodocumentation C++Type Erasure

4、open-std P1144R2 Object relocation in terms of move plus destroy

Template function signature

sodocumentation C++Type Erasure

template<class Sig>
struct task;
template<class R, class...Args>
struct task<R(Args...)> {
};

std::function is polymorphic value type

std::function 是基于 type erasure 实现的,这在 artima On the Tension Between Object-Oriented and Generic Programming in C++ 中进行了说明:

The most widely known and used examples of type erasure are boost::any[5] and boost::function[6]. I'll discuss boost::any in detail in the second part of this article. boost::function is a class template that takes one template argument, a function type. Choosing a function type amounts to(等价于) choosing a return type and a list of argument types for a function. Suppose we instantiate boost::function as follows:

boost::function<int (int)> foo;

The variable foo can now hold anything that's callable with an int as its only argument, and whose return type is convertible to int. This could be a function pointer, a user-defined functor, the result of a boost::bind, or what have you. Clearly, this matches the above definition of type erasure.

TODO wizmann C++类型擦除与std::function性能探索