Can the main function be declared like so :
template<typename T1, typename T2>
int main(T1 argc, T2 *argv[])
{
}
For an instantiation T1
= int
and T2
= char
we end up to a common signature.
The restrictions for main
mention nothing about templates :
No other function in the program can be called main
main cannot be defined as inline or static.
C++main cannot be called from within a program.
C++ The address of main cannot be taken.
C++ The main function cannot be overloaded.
Apparently there are no applications of such a syntax, but
- Is there a compiler that implements it ?
- Are there any logical barriers in implementing something like that ?
EDIT
I was a bit vague in my first attempt to asking the above. There were (rightfully) some negative remarks on the question, so I should lay down some reasoning on asking for feedback on this topic :
C++ is an evolving language, maybe this was to be implemented and someone is aware of it
Someone could inform me on why
main
has the limitations that it hasA language lawyer could find a loophole in the Standard to allow for such a declaration (well the opposite has happened)
The evolution of the modules system drives the language to a logic of component separation (in terms of compilation units for now). Maybe this will affect the way we spawn compiled units, maybe multiple
main
functions are to be defined across submodules in which case a more flexible main would be needed.
An example use case of templatizing main
If the Standard was to allow for something like that (in the future) we could write
template<typename... Args>
int main(Args&& ...vs)
{
}
there you go, safe command line arguments parsing (have I invented the wheel or what?)
It depends. In my own super-secret and private freestanding implementation you can make the main function be a template if all the following conditions apply.
mаin
(note that the second letter is a Cyrillic letter, not a Latin letter).class
, nottypename
, there is no space betweenclass
and dot-dot-dot, and there is a single space between dot-dot-dot and the pack name.All of this information is obviously completely useless in face of the question presented, but who cares? At least it mentions freestanding implementations and language extensions.
This:
is really a function template. The Standard, as per §3.6.1/1, requires
main
to be a function:Just like classes and class templates are not the same thing, function and function templates are two different things. More specifically, as per §14.1:
Therefore a function template can "generate" a potentially infinite † set of functions.
† is arguable
In principle, taking full advantage of the latitude the standard leaves to implementations, yours may decide to allow that one.
Still, I don't know any implementation which does when using
main
as the starting point, nor a reason to do so.This allows it as an extenson, useable after issuing at least one diagnostic:
This one allows it for freestanding environments even without diagnostics: