[英]What's the meaning of typedef int function(void*)?
I saw some BSD code using the following construct: 我使用以下构造看到了一些BSD代码:
typedef int driver_filter_t(void*);
What does that mean, exactly? 这究竟是什么意思? I don't think it's a function pointer because otherwise it would be something like typedef int (*driver_filter_t)(void*)
, right? 我不认为它是一个函数指针,否则它会像typedef int (*driver_filter_t)(void*)
,对吧?
typedef int driver_filter_t(void*);
This is a definition of a function type . 这是函数类型的定义。 It makes driver_filter_t
an alias for the type that can be described as "function returning int
with an argument of type pointer to void
". 它使得driver_filter_t
成为类型的别名,可以将其描述为“函数返回int
,其类型指针的参数为void
”。
As for all typedef
s, it creates an alias for an existing type, not a new type. 对于所有typedef
,它为现有类型创建别名,而不是新类型。
driver_filter_t
is not a pointer type. driver_filter_t
不是指针类型。 You can't declare something of type driver_filter_t
(the grammar doesn't allow declaring a function using a typedef name). 你不能声明类型为driver_filter_t
东西(语法不允许使用typedef名称声明一个函数)。 You can declare an object that's a function pointer as, for example: 您可以声明一个函数指针对象,例如:
driver_filter_t *func_ptr;
Because you can't use a function type name directly without adding a *
to denote a pointer type, it's probably more common to define typedef
s for function pointer types, such as: 因为您不能直接使用函数类型名称而不添加*
来表示指针类型,所以为函数指针类型定义typedef
可能更常见,例如:
typedef int (*driver_filter_pointer)(void*);
But typedefs for function types are pefectly legal, and personally I find them clearer. 但函数类型的typedef完全合法,我个人觉得它们更清晰。
typedef int driver_filter_t(void*);
is a typedef for a function type. 是函数类型的typedef。 In C you can use it for function pointers like driver_filter_t* fn_ptr
. 在C中,您可以将它用于函数指针,如driver_filter_t* fn_ptr
。
In C++ you can also use that typedef to declare member functions (but not to implement them): 在C ++中,您还可以使用该typedef来声明成员函数(但不是为了实现它们):
struct Some {
driver_filter_t foo; // int foo(void*);
driver_filter_t bar; // int bar(void*);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.