更新时间:2022-11-22 10:44:58
std :: function
(和它的灵感, boost :: function
)不仅存储函数指针。它还可以存储函数对象。在这个意义上,传递函数签名作为模板参数类似于智能指针通常采用 pointee 作为模板参数,而不是指针类型!
std::function
(and its inspiration, boost::function
) does not only store function pointers. It can also store function objects. In that sense, passing a function signature as a template parameter is similar to how a smart pointer usually take the type of the pointee as a template parameter, not a pointer type!
对比度:
int* p; // indirection to an object of type int
std::unique_ptr<int> q; // indirection to an object of type int
和
typedef void signature_type(); // a function type
// indirection to something callable with signature_type as a signature
// i.e. f() has type void
// only work for freestanding functions however
signature_type* f;
// indirection to something callable with signature_type as a signature
// i.e. g() has type void
// not restricted to function pointers!
std::function<signature_type> g;
这是一个有用的约定。