主函数可以是模板吗? (安全命令行参数解析) [英] Can the main function be a template? (safe command line argument parsing)

查看:163
本文介绍了主函数可以是模板吗? (安全命令行参数解析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主函数可以这样声明:

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

对于实例化 T1 = int T2 = char 我们结束一个共同的签名。



main 的限制不提供任何模板:




  • 程序中没有其他函数可以调用main


  • main不能定义为内联或静态。 / p>


  • C ++ main不能在程序中调用。


$ b


$ b

显然没有这样的语法的应用程序,但




  • 是否有编译器实现它?

  • 在实施类似方面是否有任何逻辑障碍



EDIT



我第一次尝试询问时有点模糊以上。对这个问题有一些否定的意见,所以我应该放下有关这个主题的反馈的一些推理:



    $


  • 有人可以通知我为什么 有其限制


  • 在标准中找到一个漏洞,以允许这样的声明(正好相反的情况发生了)


  • 模块系统的演变将语言驱动到逻辑组件分离(现在就编译单元而言)。也许这将影响我们产生编译单元的方式,也许多个 main 函数将跨子模块定义,在这种情况下,更多的灵活需要。




模板化的示例用例 main



如果标准允许使用类似的功能,我们可以写

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

你去,安全的命令行参数解析(我发明了轮子或什么?)

解决方案

这是:

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

确实是一个函数模板。根据§3.6.1/ 1,标准要求 main 是一个函数


程序应包含一个名为main的全局函数,它是程序的指定开始。


类模板不同的是,函数东西。更具体地说,根据§14.1:


模板定义了一系列类或函数或一系列类型的别名。 p>

因此,函数模板可以生成潜在的无限函数集。



†​​是可争辩的


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

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆