问题描述
有效的C ++ 主要
签名如下:
The valid C++ main
signatures are the following:
int main()
int main(int argc, char *argv[])
int main(int argc, char **argv)
但不允许声明 main
采用初始化程序列表:
But isn't allowed to declare main
taking an initializer list:
int main(std::initializer_list<char *> args)
b $ b
AFAIK初始化器列表可以实现为一对指针或一个指针(这可以是 argv
参数)加上一个长度(可以推导出来从 argc
参数),其存储可以是自动,临时或静态只读存储器。
AFAIK the initializer list could be implemented as a pair of pointers or a pointer (this could be the argv
parameter) plus a length (this could be deduced from the argc
parameter), and its storage could be automatic, temporary, or static read-only memory depending on the situation.
所以我认为一个 std :: initializer_list& >
可以处理和管理命令行参数没有任何问题,那么我想知道为什么这个假设的 main
签名没有添加批准C ++ 11标准中的初始化列表,因为我要求:
So i think that an std::initializer_list<char *>
could handle and manage without any problem the command line parameters, then i'm wondering why this hypothetical main
signature wasn't added after the approval of the initializer lists on the C++11 standard, and because of that i'm asking:
- 可能的缺点或由于允许初始化列表作为
main
only参数而导致的问题? - 这是向标准委员会提出这项增加(或任何其他变更)的正确方法?
- What could be the disadvantages or problems derived of allowing an initializer list as the
main
only parameter? (i couldn't think of any). - Which would be the correct way to propose to the standards committee this addition (or any other change)?
推荐答案
虽然有两种方式可以指定 main()
程序,当前实现的C ++运行时调用 main()
函数的方式相同(他们传递参数不管
main()
是如何声明的)。你的建议需要所有实现的C ++运行时来确定程序正在使用哪种类型的 main()
,并调用正确的 main ()
。如果你真的想要这个功能,你总是可以提供一个实现 main(int,char * [])
将参数转换为初始化列表像对象向量<>
),然后调用您选择的新入口点函数。
Although there are two ways of specifying main()
in a program, most (all?) current implementations of the C++ run-time call the main()
function the same way (they pass the arguments as (int, char *[])
irrespective to how main()
is declared). Your proposal would require the C++ run-time of all implementations to figure out which kind of main()
the program is using, and call the right main()
. If you really want this functionality for yourself, you can always provide an implementation of main(int, char *[])
that converts the arguments into an initializer list like object (such as vector<>
), and then calls a new entry point function of your choice.
在上提交提案。基本步骤是:(1)将其想法浮在其Usenet组/邮件列表上; (2)起草提案,征求反馈意见,并相应更新提案;和(3)重复该过程直到接受该提案。
The process for submitting a proposal is described at the Standard C++ website. The basic steps are: (1) float the idea on their Usenet group/mailing list; (2) draft a proposal, solicit feedback, and update the proposal accordingly; and (3) repeat the process until the proposal is accepted.
这篇关于为什么初始化列表不能是main的参数?如何提出呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!