Need a quote from the standard on the legality of the main function as a template function

On a whim, I tried to define the main function as a template function using clang 2.9:

template <typename T = void> int main(int argc, char **argv) { } 

and got the following error.

 error: 'main' cannot be a template int main(int argc, char **argv) ^ 

Does anyone know which part of the standard prohibits this, and which text do you have?

+7
source share
2 answers

Well, how about this (3.6.1):

The program must contain the global function main, which is the designated start of the program. [...] This function should not be overloaded. It must have a return type of int, but otherwise its type is determined by the implementation.

Since templates are not functions, I don’t think you have a choice in this matter. In particular, the function should be main , not main<> , as in your example; and your main not a function, but a template that rules out the existence of another function called main .

+15
source

Function templates must be declared in the .h file.

-7
source

All Articles