[英]difference between void(int) & void (*)(int)
I know void (*)(int)
is to function pointer but what is void(int)
? 我知道
void (*)(int)
是函数指针但是什么是void(int)
?
It's used for std::function
template. 它用于
std::function
模板。
Say I have a function void fun(int){}
: decltype(&fun)
gives void(*)(int)
but decltype(fun)
gives void(int)
假设我有一个函数
void fun(int){}
: decltype(&fun)
给出void(*)(int)
但是decltype(fun)
给出void(int)
If T
is a type, then T*
denotes the type "pointer-to- T
". 如果
T
是类型,则T*
表示“指向T
指针”类型。
The type void(int)
is a function type , it's the type of a function taking one int
and returning void
. void(int)
类型是一个函数类型 ,它是一个带有一个int
并返回void
的函数的类型。 For example, it is the type of f
if f
is declared as void f(int);
例如,它是类型
f
如果f
被声明为void f(int);
If T = void(int)
, then T*
is spelled void(*)(int)
, so the latter is the type of a function pointer . 如果
T = void(int)
,则T*
拼写为void(*)(int)
,因此后者是函数指针的类型。 You can also form a reference to a function, which is T& = void(&)(int)
; 您还可以形成对函数的引用,即
T& = void(&)(int)
; this is occasionally more useful (eg you can take the address of a function lvalue). 这有时更有用(例如,您可以获取函数lvalue的地址)。
Aside note: Function lvalues decay to their function pointer very easily. 除了注意:函数左值很容易衰减到它们的函数指针。 You can call a function either via a function lvalue or via a function pointer.
您可以通过函数左值或函数指针调用函数。 When used as an operand for the indirection operator (
*
), the function value decays, so you can dereference the pointer again and again: 当用作间接运算符(
*
)的操作数时,函数值会衰减,因此您可以反复取消引用指针:
printf("Hello world\n"); // OK
(*printf)("Hello world\n"); // also OK
(****printf)("Hello world\n"); // four-star programmer
Some of the only times that a function does not decay is when used as the operand of the address-of operator, or when bound to a reference: 函数不衰减的一些唯一时间是用作address-of运算符的操作数,或者绑定到引用时:
void f(int); // our example function
void(*p1)(int) = &f; // no decay of "f" here
void(*p2)(int) = f; // "f" decays
void(&r1)(int) = f; // no decay of "f" here
void g(void(&callback)(int), int n) {
callback(n);
}
g(f, 10); // no decay of "f" here
template <typename F, typename ...Args>
decltype(auto) h(F&& callback, Args&&... args) {
return std::forward<F>(callback)(std::forward<Args>(args)...);
}
h(f, 10); // no decay of "f" here
void (*whatever)(int)
should be read as: whatever is a pointer, pointing to a function, that accepts one int as argument, and returns nothing (ie., void). 应该被理解为:无论是指向函数的指针,都接受一个int作为参数,并且不返回任何内容(即,void)。
void whatever(int)
should be read as: whatever is a function (NOT a pointer), that accepts one int as argument, and returns nothing (ie., void) 应该被理解为:无论是函数(不是指针),接受一个int作为参数,并且什么都不返回(即,void)
Once the pointer to a function is initialized to point to a valid function (one that satisfies the prototype), then you can invoke the function either through its "real" name, or through the pointer. 一旦指向函数的指针被初始化为指向有效函数(满足原型的函数),那么您可以通过其“真实”名称或通过指针调用该函数。
Pointers to functions are very useful - they're variables, just like anything else, so you can pass them around to other functions (see eg qsort()), you can put them in structs, etc.. 指向函数的指针非常有用 - 它们是变量,就像其他任何东西一样,所以你可以将它们传递给其他函数(参见例如qsort()),你可以将它们放在结构体中等等。
Given this, the following code is valid: 鉴于此,以下代码有效:
#include <stdio.h>
void myfun(int x) {
printf("The value of X is %d\n", x);
}
int main() {
void (*myfunp)(int);
myfunp = &myfun;
myfun(13);
myfunp(12);
return 0;
}
void(*)(int)
should be read as type of a pointer which is pointing to a function
, that accepts one int
as argument, and returns nothing. void(*)(int)
应该被读作指向function
的指针的类型,它接受一个int
作为参数,并且不返回任何内容。
For understanding more on function to pointer and its usage please check here: http://www.cprogramming.com/tutorial/function-pointers.html 有关指针功能及其用法的更多信息,请访问: http : //www.cprogramming.com/tutorial/function-pointers.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.