I was just messing around with some function pointers after re-reading Effective STL and it's comments on C++'s most vexing parse. In that chapter Scott Meyers mentions how in C++ as well as function pointers you can have references to functions too:
void (&function)();
And also that you can omit the pointer operator in function pointers passed as arguments - it is implicit:
void function1(void (function2)());
So I was messing around with typedef's of functions to see what could be done. One of the nifty but pointless things I found was that you can declare a function pointer by doing:
typedef void (function)();
function* functionPointer;
But imagine my surprise when I discovered that both GCC and Borland allow you to use this function typedef for forward declarations:
typedef void (function)();
function someFunction;
void anotherFunction()
{
someFunction();
}
void someFunction()
{
}
And then my complete bafflement to discover that Borland (but not GCC) can actually compile the following:
#include <iostream>
typedef int (function)(int value);
function square
{
return value * value;
}
function absoluteValue
{
if (value < 0)
{
return -value;
}
return value;
}
int main()
{
std::cout << square(5) << '\n';
function* functionPointer = absoluteValue;
std::cout << functionPointer(-7) << '\n';
}
Now is that funky or what!
Now the big question does anybody have a copy of the standard or a sufficiently advanced knowledge of the language to tell me if any of this is standards compliant? I'm guessing that using a function typedef to declare a function pointer:
typedef void (function)();
function* functionPointer;
is standards compliant and the other stuff isn't, but I'd love to know for sure.
Enigma