Issue
Take the following code:
int main()
{
decltype(main()) x = 0;
return x;
}
gcc complains:
main.cpp: In function 'int main()':
main.cpp:8:19: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]
decltype(main()) x = 0;
^
main.cpp:8:19: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]
but not clang. So what about decltype(main())
raises this error? How does decltype
take the address of main?
Solution
GCC's diagnostic might not be correctly phrased in this case, because decltype
doesn't need to know the address of main
; it only needs to know its type. However, the warning is based on the following from the standard (§3.6.1/3):
The function
main
shall not be used within a program.
I suppose GCC interprets this to mean that you can't even use it in an unevaluated expression.
Clang (version 3.4 anyway) appears to not implement this rule at all, even if I turn on all the flags I can think of and even if main
calls itself recursively. That's why it doesn't give you a warning.
Answered By - Brian Bi Answer Checked By - Timothy Miller (WPSolving Admin)