$3.6.1/3 states-
“The function main shall not be used
(3.2) within a program.”.
The sample academically motivated program below uses the name ‘main’ in a couple of ways which I thought are legitimate. This is based on the assumption that ‘usage of a function’ is related to calling the function (directly/indirectly).
struct MyClass{
private:
MyClass(){}
friend int main();
};
int main(){
MyClass m;
int (*p)() = main; // but don't do anything
}
-
Fair enough, the code shown compiles with gcc/VS 2010.
-
I am surprised by Comeau’s error.
Comeau online gives error while declaration ‘p’ (i.e while taking address of ‘main’) but not while declaraing ‘main’ as a friend.
Who/What is right with respect to C++03?
C++03 §3.2/2 says:
It goes on to list what constitutes use of other various types of entities; this is the important one here.
A friend declaration is not an expression.
When the function
main()is converted to a pointer and that pointer is assigned top, that is an expression and it is potentially evaluated (C++03 §3.2/2):