7

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 received the following error.

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

Does anyone know what section of the standard forbids this, and what the relevant text is?

Michael Price
  • 8,088
  • 1
  • 17
  • 24
  • Regardless of what the standard says, how do you expect the linker to know that `main` is your entry point? – Dennis Zickefoose Nov 16 '11 at 04:49
  • In my original post, I incorrectly stated that I was using clang 2.0, when it should have said clang 2.9. I've since corrected that. – Michael Price Nov 16 '11 at 11:48
  • @DennisZickefoose - Well, I was kind of hoping that the fact that it was named `main` would be a hint. Obviously the `` is problematic, but since this is a function template, we any other case we could modify it some to use type inference so that it could be called without the explicit template goo. – Michael Price Nov 16 '11 at 11:58
  • possible duplicate of [Is it legal to recurse into main() in C++?](http://stackoverflow.com/questions/4518598/is-it-legal-to-recurse-into-main-in-c) – user Sep 09 '13 at 12:17

2 Answers2

15

Well, how about this (3.6.1):

A program shall contain a global function called main, which is the designated start of the program. [...] This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.

Since templates are not functions, I don't think you have any choice in the matter. In particular, the function has to be main, not main<> as in your example; and your main isn't a function, but a template, precluding the existence of another function called main.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    I dunno, the Standard does sometimes say that function template specializations are functions. 14.5.6/1 "A function template defines an unbounded set of related functions." 14.5.6.1/2 "Such specializations are distinct functions and do not violate the one definition rule." – aschepler Nov 16 '11 at 04:33
  • 5
    @aschepler: no doubt, but those functions are called `main` and `main` etc, and not `main`. All templates constitute an unbounded familiy of related types, but those types are distinct from the template itself. – Kerrek SB Nov 16 '11 at 04:34
  • @KerrekSB - Yeah, I think in this situation, the fact that what I've written is a function template, not a function is important. See http://stackoverflow.com/questions/1117755/what-is-the-difference-between-function-template-and-template-function – Michael Price Nov 16 '11 at 11:53
  • @KerrekSB: I'm certain the reason you give *in your comment* is the right answer, but the reason in your actual answer is not! Please edit. – j_random_hacker Nov 16 '11 at 12:04
  • @Michael Price: But either way, it would not work. The template does nothing since it's not instantiated. But if you do instantiate it, then it's an overload (except if you _accidentially_ instantiate it with `int`). So either way it's violating the standard's "shall". But even if you promise that you only plan to instantiate the template with `int` (which is somewhat absurd, why a template in the first place then?), how can the compiler be sure? – Damon Dec 06 '13 at 12:31
  • @Damon: Actually, templates themselves are part of overload resolution, not just the functions instantiated from them. During overload resolution, function templates are being considered and template argument deduction is performed (if applicable) to determine the set of viable instantiations. – Kerrek SB Dec 06 '13 at 12:48
-7

Function templates must be declared in .h file.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 1
    Even if that were true, it wouldn't answer the question. – JoeG Nov 16 '11 at 11:52
  • 1
    The preprocessor knows nothing about templates. When it gets to the lexer, all that matters is that the declaration and definition of the template must be in the same translation unit. – Michael Price Nov 16 '11 at 11:55