Lightness Races in Orbit Lightness Races in Orbit - 3 months ago 18
C++ Question

Why can the return type of main not be deduced?

As expected, the following fails in C++11 because that language does not have return type deduction for bog standard functions:

auto main()
{
return 0;
}


However, C++14 does, so I cannot explain the following error (with equivalent outcomes in GCC trunk, clang 3.8 and Visual Studio 2015):

error: 'main' must return 'int'


Is there a passage in the standard that I'm not seeing, forbidding return type deduction for
main
? Or are both compilers non-compliant?

(For what it's worth, I'd never actually do this.
int main()
for the win…)

Answer

Reading the C++17 draft ยง3.6.1/2:

... and it shall have a declared return type of type int, ...

So yes I would say it's forbidden to use deduction.


Almost the exact same wording in the last C++14 draft (same section as the C++17 draft):

It shall have a declared return type of type int, ...