Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...