Could the main function be a template? (parsing command line arguments)

Is it possible to declare the main function as follows:

template<typename T1, typename T2> int main(T1 argc, T2 *argv[]) { } 

For the instance T1 = int and T2 = char we end the general signature.

Constraints for main mention nothing about templates:

  • No other function in the program can be called the main

  • main cannot be defined as inline or static.

  • C ++ main cannot be called from within a program.

  • C ++. The primary address cannot be completed.

  • C ++ The main function cannot be overloaded.

Apparently, there is no such syntax application, but

  • Is there a compiler that implements it?
  • Are there any logical barriers to implementing something like this?

EDIT

I was a little vague in my first attempt to ask above. There were (by right) some negative comments on this issue, so I have to formulate some arguments regarding the request for feedback on this topic :

  • C ++ is an evolving language, perhaps it should have been implemented, and someone knows about it

  • Someone can tell me about why main has limitations that it has

  • A language attorney may find a loophole in the Standard to allow such a declaration (the opposite happened well)

  • The evolution of the module system leads the language to the logic of separation of components (in terms of compilation units at the moment). Perhaps this will affect the way we create compiled units, maybe several main functions should be defined through submodules, in which case a more flexible main one will be needed.

Templatizing main example

If the Standard allowed something like this (in the future), we could write

 template<typename... Args> int main(Args&& ...vs) { } 

you go, the arguments of the arguments on the command line (did I invent the wheel or what?)

+6
source share
3 answers

It:

 template<typename T1, typename T2> int main(T1 argc, T2 *argv[]) { } 

is truly a function template. The standard, according to §3.6.1 / 1, requires main as a function:

The program must contain the global function main, which is the designated start of the program.

Just like classes and class templates are not the same thing, function templates and functions are two different things. More specifically, according to §14.1:

A template defines a family of classes or functions or an alias for a type family.

Therefore, a function template can “generate” a potentially infinite set of functions .

† it can be argued

+17
source

It depends. In my own top-secret and private autonomous implementation, you can make the main function a template if all of the following conditions apply.

  • The function is called min (note that the second letter is a Cyrillic letter, not a Latin letter).
  • There are either 2, 17, or 23 template arguments without a package, or one variation package.
  • If the template arguments consist of a variational package, it is entered with class , not typename , there is no space between class and dot-dot-dot, and between dot-dot-dot and the package name.
  • This is a full moon, and you are in the Northern Hemisphere, or you are in the Southern Hemisphere, and Mars is above the horizon. (Compilation without GPS or ASCOM is not supported.)

All this information is obviously completely useless in the face of the question presented, but who needs it? At least he mentions autonomous implementations and extensions of the language.

+16
source

In principle, using the full breadth, the standard goes to implementation, yours may decide to allow it.

However, I do not know any implementation that occurs when using main as a starting point, as well as the reasons for this.

This allows you to use it as extenson, which can be used after issuing at least one diagnosis:

1.4 Compliance with the implementation of §8

The corresponding implementation may have extensions (including additional library functions) provided that they do not change the behavior of any well-formed program. Implementations are needed to diagnose programs that use such extensions that are poorly formed in accordance with this International Standard. However, by doing this, they can compile and execute such programs.

This allows you to use it for stand-alone environments even without diagnostics:

3.6.1 Main function [basic.start.main]

1 The program must contain the global function main, which is the designated start of the program. The implementation determines whether the program is required in a stand-alone environment to determine the core function. [Note. In a stand-alone environment, start-up and termination are determined by position; startup contains executing constructors for namespace scope objects with static storage duration; completion contains the execution of destructors for objects with static storage duration. -end note]

+2
source

All Articles