   Function pointers - No argument - Odd result
  Recently when learning function pointer use i found the following:cout << fp2 << endl; results were unexpected as 1, given that no arguments were given i expected the function to compute as if x,y,z was 0.So my question, is why 1? Is it a consequence of non-initialisation or due to the ostream?#include <iostream>using namespace std;//Define a functionvoid function1( char* pCh, int n );int blah( int x, int y, int z );//define another function using a function pointer as argumentvoid bunction( int (*pleb) (int, int, int) );int main(){    cout << "Hello world!" << endl;    //define a a pointer to a function    void ( *fp ) ( char*, int );//Associate function to pointer    fp = &function1;    //Call the pointed to function and pass it the parameters    fp( "Test", 10 );    //Passing a function pointer as a function argumentint (*fp2)( int, int, int );fp2 = &blah;cout << fp2( 1, 2, 3 ) << endl;cout << fp2 << endl;cout << fp2(0,0,0) << endl;cout << (*fp2) << endl;bunction( fp2 );    return 0;}void function1( char* pCh, int n ){cout << "char=" << pCh << " int=" << n << endl;}int blah( int x, int y, int z ){return (x + y + z );}void bunction( int (*pleb) (int, int, int) ){cout << (*pleb)(3,3,3) << endl;}Output:Hello world!char=Test int=1061  <----- Note01  <-----9
Quote:Original post by BlackSpotcout << fp2 << endl; results were unexpected as 1, given that no arguments were given i expected the function to compute as if x,y,z was 0.Incorrect expectation. The function will only compute if you call it. Writing cout << fp2 << endl does not call the function, it merely outputs the value of the function pointer (in an obviously implementation-defined way converted to a boolean). In C++, a function is only called when you provide its argument list, between brackets, and you are not allowed to "forget" an argument unless that argument has a default value.[Edited by - ToohrVyk on September 29, 2008 7:12:08 AM];  A hint: cout << &fp2 << endl; is the portable form. Using the function name purely (without the '&') is (afaik) not defined in the standards (and drived me crazy once when I switched from msvcpp to another compiler).Argh, sorry. When I first glimpsed on your code I thought "fp" would be your function name. Of course it is correct valid to do:void function1( char* pCh, int n ) {}void ( *fp ) ( char*, int ) = &function1;cout << fp << endl; // output value of the variable fpcout << &fp << endl; // output adress of the variable fpMea Culpa.[Edited by - greenhybrid on September 29, 2008 9:18:26 AM];  Yeah when setting the function pointer i use fp = &function; it just seems more correct anyway.The reason i didnt use cout << &fp2 << endl; was because that wasnt my intent :) even though that is what i got.Quote: In C++, a function is only called when you provide its argument list, between brackets, and you are not allowed to "forget" an argument unless that argument has a default value.Yeah, thats what i thought. Simply a lack of defaults.   ;Quote:Original post by ToohrVykWriting cout << fp2 << endl does not call the function, it merely outputs the value of the function pointer (in an obviously implementation-defined way).Actually what is happening is that the function pointer is being implicitly converted to a bool.   ;Quote:Original post by SiCraneQuote:Original post by ToohrVykWriting cout << fp2 << endl does not call the function, it merely outputs the value of the function pointer (in an obviously implementation-defined way).Actually what is happening is that the function pointer is being implicitly converted to a bool.Why? From my experience it would dump something in pointer format (e.g. '0x0000000000000000'), and from this I bet it will be converted to void*, together with a warning by the compiler (though I don't have a compiler handy).edit: Okay, I see ToohrVyk also 'falls back'. Now I am curious. So does Standard-C++ convert pointers to bool, if no proper overload is available (like with "if (fp2)")?   ;Quote:Original post by greenhybridWhy? From my experience it would dump something in pointer format (e.g. '0x0000000000000000'), and from this I bet it will be converted to void*, together with a warning by the compiler (though I don't have a compiler handy).My mistake was assuming that operator<< was defined for function pointers, which it isn't. A function pointer cannot be implicitly converted to void* (so the compiler wouldn't try it), so a boolean is the only possibility.;Quote:Original post by greenhybridand from thisI really wouldn't recommend using that site as a reference. It has numerous inaccuracies all over the place. For example, on that page, it lists ostream& operator<< (bool& val ); as one of the ostream operator << overloads, but the actual signature is ostream& operator<< (bool val);.  Repeat for short, int, etc.   ;Quote:Original post by ToohrVykMy mistake was assuming that operator<< was defined for function pointers, which it isn't. A function pointer cannot be implicitly converted to void* (so the compiler wouldn't try it), so a boolean is the only possibility.Unfortunately, operator << is defined for some function pointers, because the std::endl, std::flush, and all the other stream manipulators are defined as function pointers (at least in libstdc++). In general I would avoid printing a function pointer without an explicit cast to void *.   ;Quote:Original post by ToohrVykQuote:Original post by greenhybridWhy? From my experience it would dump something in pointer format (e.g. '0x0000000000000000'), and from this I bet it will be converted to void*, together with a warning by the compiler (though I don't have a compiler handy).My mistake was assuming that operator<< was defined for function pointers, which it isn't. A function pointer cannot be implicitly converted to void* (so the compiler wouldn't try it), so a boolean is the only possibility.For some reasons I have the feeling that (wrt to the native bool type of c++, and the stronger typing of c++ compared to C) the conv-2-bool is as wrong as any other conversion there.Is that behaviour defined in the standards or was it just trying out in vcpp?   