Now that I think of it, taking the address of main() in a C++ program
is illegal no matter what you do with the address.
It compiles; I wrote it in VC++.NET and it works with no problems. BTW, I
know that the & wasn't necessary, but there's nothing wrong with making it
obvious that an address is being passed.
Apparently you are not using a real C++ compiler, or you are not using
the one you have in a standard conforming mode.
First you should have received diagnostics along these lines:
========
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
simple.cpp:
"simple.cpp": E2012 Cannot take address of 'main' in function main()
at line 16
"simple.cpp": E2012 Cannot take address of 'main' in function main()
at line 17
*** 2 errors in Compile ***
BCC32 exited with error code: 1
Build cancelled due to errors
========
Interestingly enough when I modify your source code to remove the
illegality of taking the address of main, like this:
========
#include <iostream>
using std::cout;
void doThatStuff(void* vp);
template<class T>
void doThisStuff(T* ptr)
{
cout << "Error. doStuff(T* ptr) called from address " << ptr <<
"\n";
}
void my_main()
{
doThisStuff(my_main);
doThatStuff(my_main);
}
int main()
{
my_main();
return 0;
}
void doThatStuff(void* vp)
{
cout << "Error. doStuff(void* vp) called from address " << vp <<
"\n";
}
========
Borland's C++ Builder X accepts it without complaint, as does
Microsoft's Visual C++ 2005 Express Beta. The MINGW 3.2 included with
C++ Builder X generates a proper diagnostic:
========
C:\prog\CBuilderX\mingw\bin\g++ -c -o
C:\prog\CBuilderX\projects\simple2\windows\Debug_Build\simple2.cpp.obj
-g2 -O0 -MD -BC:\prog\CBuilderX\MinGW\bin
-IC:\prog\CBuilderX\mingw\include
-IC:\prog\CBuilderX\mingw\include\c++\3.2
windows\Debug_Build\simple2.cpp.cpp
windows/Debug_Build/simple2.cpp.cpp: In function `void my_main()':
"simple2.cpp.cpp": windows/Debug_Build/simple2.cpp.cpp invalid
conversion from `void (*)()' to at line 17
`void*'
Build cancelled due to errors
========
Testing it online with Comeau Computing's EDG front-end, arguably the
most standard conforming implementation readily accessible, also
results in a correct, and better presented, diagnostic:
========
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++
"ComeauTest.c", line 17: error: argument of type "void (*)()" is
incompatible with
parameter of type "void *"
doThatStuff(my_main);
^
1 error detected in the compilation of "ComeauTest.c".
========
Note that ISO/IEC 14882 determines what is and is not legal C++, not
just what some compilers happen to accept. In particular, Windows
based compilers (Microsoft and Borland) seem to let a lot slip by,
such as an implicit conversion from "pointer to function returning
void and accepting no arguments" to "pointer to void".
The fact that compiler accepted it without issuing a diagnostic only
means that there is a serious error in your compiler. The fact that
it allowed you to take the address of main() in a C++ program is
another serious error. Microsoft's C++ compilers prior to 7.1 did not
put a great deal of emphasis on C++ standard conformance.
Regardless of what you think, the sample code you posted had two
serious errors.