I\'ve been experimenting with function pointers and found the behavior of the following program rather misterious:
void foo(int(*p)())
{ std::cout << p
std::cout << p << std::endl;
here an overload of operator<<
which accepts a bool
is picked up:
basic_ostream& operator<<( bool value );
As p
is not null, then 1
is printed.
If you need to print an actual address, then the cast is necessary, as others mention.
Your function pointer is cast to a bool
which is true
, or 1
without std::boolalpha
.
If you want to see the address you can cast it:
std::cout << static_cast<void*>(p) << std::endl;