Why can you call C/C++ functions like this:
(&myAwesomefunction)(arg1,arg2,arg3...);
Surely taking the address of a function gives you a pointer/address. And as I understand it pointers are not callable as functions. What’s really happening?
Does it mean that I can call any address like a function?
// Is this legal?
int x = 69;
(&x)(3,78,69.456,'c','o','n','d','o','m');
What are the implications of THAT?
On the contrary, pointers to functions are the only things callable as functions. The definition of the function call operator starts out:
(This means that your second hypothetical is not legal, because
&xis does not have a type that is a pointer to a function of any sort).So, you might ask, why can you also call a function like this (where
myAwesomefunctionis an identifier declared as a function)?The answer is that a identifier like
myAwesomefunctionis a primary expression that evaluates to a function designator. The definition of a function designator says:So formally, when you write
myAwesomefunction(arg1,arg2,arg3...);,myAwesomefunctionevaluates to a function designator, which then is converted to a pointer to the function, which is then used to call the function.This conversion of a function designator to a pointer to the function is similar to the conversion of an expression with array type to a pointer to the array’s first element.