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 has
A 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?)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…