I know void (*)(int)
is to function pointer but what is void(int)
?
It's used for std::function
template.
Say I have a function void fun(int){}
: decltype(&fun)
gives void(*)(int)
but decltype(fun)
gives void(int)
I know void (*)(int)
is to function pointer but what is void(int)
?
It's used for std::function
template.
Say I have a function void fun(int){}
: decltype(&fun)
gives void(*)(int)
but decltype(fun)
gives void(int)
If
T
is a type, thenT*
denotes the type "pointer-to-T
".The type
void(int)
is a function type, it's the type of a function taking oneint
and returningvoid
. For example, it is the type off
iff
is declared asvoid f(int);
If
T = void(int)
, thenT*
is spelledvoid(*)(int)
, so the latter is the type of a function pointer. You can also form a reference to a function, which isT& = void(&)(int)
; this is occasionally more useful (e.g. you can take the address of a function 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: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:
should be read as: whatever is a pointer, pointing to a function, that accepts one int as argument, and returns nothing (ie., void).
should be read as: whatever is a function (NOT a pointer), that accepts one int as argument, and returns nothing (ie., 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 e.g. qsort()), you can put them in structs, etc..
Given this, the following code is valid:
void(*)(int)
should be read as type of a pointer which is pointing to afunction
, that accepts oneint
as argument, and returns nothing.For understanding more on function to pointer and its usage please check here: http://www.cprogramming.com/tutorial/function-pointers.html