test is a pointer to function, isn't it?
test
is not a pointer to a function. It is a function.
To make the difference between a function and a function pointer more visible consider the following code snippet.
using Fn = void();
using FnPtr = void ( * )( );
Fn test;
FnPtr test_ptr = test;
void test()
{
int a = 100;
std::cout << a << std::endl;
}
In this code snippet there are introduced two aliases. The first alias Fn
denotes the function type void()
. The second alias FnPtr
denotes the pointer type void( * )()
.
So this record
Fn test;
declares the function test
while this record
FnPtr test_ptr = test;
declares the function pointer test_ptr
that is initialized by the address of the function test because the function designator test
used as an initializer in this declaration is implicitly converted to a pointer to the function test
. You could rewrite the declaration also the following way
FnPtr test_ptr = &test;
explicitly applying the address of operator.
These two function call expressions
(*b)();
b();
differ only in one aspect.
In the first call there is used a function lvalue. Neither implicit conversion from the lvalue to a function pointer is done.
In the second call there is used a function pointer.
From the C++ 14 Standard (5.2.2 Function call)
- ...For a call to a non-member function or to a static member function,
the postfix expression shall be either an lvalue that refers to a
function (in which case the function-to-pointer standard conversion
(4.3) is suppressed on the postfix expression), or it shall have
pointer to function type
Pay attention to that you could call the function also the following way
( *test )();
In this case the function designator is implicitly converted to a pointer to the function and then after applying the operator * an lvalue to the function is obtained. S you may even write
( ******test )();
or
( *&*&*&test )();