[英]Syntax of C++ templates with function type parameters
I'm used to seeing syntax like this for function pointers 我习惯于在函数指针中看到这样的语法
int (*pointer_name) (float, char *);
void call_function (void (*)(int), int);
In some C++03 functional libraries I see types used this way: 在一些C ++ 03函数库中,我看到以这种方式使用的类型:
abc::function<void(*)(int,float)> f;
In C++11's std::function
I see the type given this way 在C ++ 11的std::function
我看到了这种方式给出的类型
std::function<void(int,float)> f;
There is a missing (*)
. 缺少(*)
。 Why? 为什么?
The C++03 function<T>
has T
being an identical type to the corresponding function pointer. C ++ 03 function<T>
T
与相应的函数指针的类型相同。 It's easy to imagine the implementation. 很容易想象实现。
std::function
in C++11 is supported by core language enhancements. 核心语言增强支持C ++ 11中的std::function
。 Have template argument types been extended to accomodate callability? 是否已扩展模板参数类型以适应可调用性?
std::function
(and its inspiration, boost::function
) does not only store function pointers. std::function
(及其灵感, boost::function
)不仅存储函数指针。 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! 从这个意义上说,将函数签名作为模板参数传递类似于智能指针通常将指针对象的类型作为模板参数,而不是指针类型!
Contrast: 对比:
int* p; // indirection to an object of type int
std::unique_ptr<int> q; // indirection to an object of type int
with 同
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;
This is a useful convention. 这是一个有用的约定。
There is nothing magic here, the type 这里没有什么神奇的类型
void(int,float)
is the type of a function without the names. 是没有名称的函数的类型。 It matches a function like void g(int x, float y)
. 它匹配像void g(int x, float y)
这样的函数。
With templates you don't have to use function pointers, you can use function types as well. 随着模板您不必使用函数指针,你可以使用函数类型为好。
As with other elements, functions have a type, and you can use either the type or the pointer to the type in different contexts. 与其他元素一样,函数具有类型,您可以在不同的上下文中使用类型或指向该类型的指针。 The missing (*)
you are expecting is just the pointer-to syntax. 您期望的缺失(*)
只是指向语法的指针 。
int (*pointer_name) (float, char *);
typedef int my_function_type(float,char*);
my_function_type * pointer_name2;
The types of pointer_name
and pointer_name2
are the same: pointer to a function that returns int
and takes two arguments of types float
and char*
. pointer_name
和pointer_name2
的类型是相同的: 指向返回int
的函数的指针,并且接受float
和char*
类型的两个参数 。 Note that this is exactly equivalent to other types like int
, with the difference that you cannot declare a variable to be of type function , only pointer to function . 请注意,这与其他类型(如int
)完全等效,区别在于您不能将变量声明为类型函数 ,只是指向函数的指针 。
The interface of std::function
(or boost::function
) just takes the signature of the function. std::function
(或boost::function
)的接口只接受boost::function
的签名 。 The type argument is not a pointer to function but rather the type of a function (like my_function_type
in the code above) type参数不是指向函数的指针,而是函数的类型(如上面代码中的my_function_type
)
Function types aren't new in C++11 (see 8.3.5 in C++98). 函数类型在C ++ 11中并不新鲜(参见C ++ 98中的8.3.5)。 IIRC, the improvement over what TR1 and boost provide for function
are quite minor. IIRC,对TR1和增强function
的改进是非常小的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.