g++ “calling” a function without parenthesis (not f() but f; ). Why does it always return 1?

You’re not actually calling pr in your code, you’re passing the function pointer to cout. pr is then being converted to a bool when being passed to cout. If you put cout << boolalpha beforehand you will output true instead of 1.

EDIT:
With C++11 you can write the following overload:

    template <class RType, class ... ArgTypes>
    std::ostream & operator<<(std::ostream & s, RType(*func)(ArgTypes...))
    {
        return s << "(func_ptr=" << (void*)func << ")(num_args=" 
                 << sizeof...(ArgTypes) << ")";
    }

which means the call cout << pr will print (func_ptr=<address of pr>)(num_args=0). The function itself can do whatever you want obviously, this is just to demonstrate that with C++11’s variadic templates, you can match function pointers of arbitrary arity. This still won’t work for overloaded functions and function templates without specifying which overload you want (usually via a cast).

Leave a Comment