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
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.
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?)
main cannot be a function template; it must be a function.
Anyway - no, you can't do this. main() cannot be a template function.
The template function works for int and char, but not float and string.
This:
template<typename T1, typename T2>
int main(T1 argc, T2 *argv[])
{
}
is really a function template. The Standard, as per §3.6.1/1, requires main
to be a function:
A program shall contain a global function called main, which is the designated start of the program.
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:
A template defines a family of classes or functions or an alias for a family of types.
Therefore a function template can "generate" a potentially infinite † set of functions.
† is arguable
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
, not typename
, there is no space between class
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.
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:
1.4 Implementation compliance §8
A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.
This one allows it for freestanding environments even without diagnostics:
3.6.1 Main function [basic.start.main]
1 A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. —end note ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With