Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the main function be a template? (safe command line argument parsing)

Tags:

c++

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?)

like image 909
Nikos Athanasiou Avatar asked May 08 '14 22:05

Nikos Athanasiou


People also ask

Can we use template in main function?

main cannot be a function template; it must be a function.

Can we use template in main function in C++?

Anyway - no, you can't do this. main() cannot be a template function.

Does function template work with string?

The template function works for int and char, but not float and string.


3 Answers

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

like image 136
Shoe Avatar answered Oct 21 '22 03:10

Shoe


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.

  • The function is named mаin (note that the second letter is a Cyrillic letter, not a Latin letter).
  • There are either 2, 17, or 23 non-pack template arguments, or a lone variadic pack.
  • If the template arguments consist of a variadic pack, it is introduced with 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.
  • It's a full moon and you're in the Northern hemisphere, or you're in the Southern hemisphere and Mars is above the horizon. (Compilation without GPS or ASCOM is not supported.)

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.

like image 20
R. Martinho Fernandes Avatar answered Oct 21 '22 03:10

R. Martinho Fernandes


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 ]

like image 42
Deduplicator Avatar answered Oct 21 '22 02:10

Deduplicator