.oisyn said:
Why the refence and the local static?
Stupidity.
I thought it would be nice to look at the assembler output so we could see that the function actually returns a pointer to itself (all that dummy class stuff is just forcing C++ to do what you want). This is the optimized output of vs.net 2003 (btw. f() now returns dummy by value):
int main(int argc, char*argv[]) {
FuncPtr fp1 = f(); // calls f()
00401020 mov eax,dword ptr [__imp_std::cout (403024h)]
00401025 sub esp,8
00401028 push offset string "Called f().\n" (40314Ch)
0040102D push eax
0040102E call dword ptr [__imp_std::operator<< (403020h)]
FuncPtr fp2 = fp1(); // also calls f()
00401034 lea ecx,[esp+8]
00401038 push ecx
00401039 call f (401000h)
FuncPtr fp3 = fp2(); // guess what ;)
0040103E lea edx,[esp+10h]
00401042 push edx
00401043 call dword ptr [eax]
00401045 add esp,10h
__asm int 3;
00401048 int 3
return 0;
00401049 xor eax,eax
};
If you know your assembler you'll see that f() returns a pointer to itself. However, the first call to f() is inlined, the second time it calls f() directly instead of through a pointer and the third time it calls f() through a pointer. I know why this happends, but it's also kind of funny.
If anyone asks why I'm having an "__asm int 3" in there: It's an habit from the old days when using a debugger that wouldn't break in release builds...