为什么C ++中的main()没有重载以使用std :: string? [英] Why main() in C++ is not overloaded to use std::string?

查看:164
本文介绍了为什么C ++中的main()没有重载以使用std :: string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用main()函数参数通过命令行获取参数的程序.作为一个(基本的)C ++程序员(即使我非常了解C风格的指针和数组),我几乎从未使用过char*字符串和C数组.我花了一些钱来接受main()参数并在std::string中对其进行转换...所以问自己:为什么在C ++中,main()函数没有重载以接受std::vector<std::string> argv而不是旧的char* argv[]?

对于重载",我指的是main()函数(如int main()int main(int argc, char *argv[]))的共存,而不是程序员编写的正常函数的重载.

解决方案

为什么它不在标准中?简单:

因为没有人提出他们的建议.

之所以进入C ++标准,是因为有人为它编写了一个提案,然后让其他人对该提案进行投票并纳入它.除非有人真的想要发生,否则它不会发生.

并考虑此功能的真正重要性:

int main(int argc, char **argv)
{
    std::vector<std::string> args(argv, argv + argc);
    ...
}

没有任何真正的需求.这是一种便利功能,与其他替代品相比,它甚至没有带来任何便利.

I was trying to create a program that takes arguments by command line, using main() function arguments. As a (basic) C++ programmer (even if I know quite well pointers and array in C-style) I hardly ever used char* strings and C-arrays. I spent some to take main() arguments and transform it in std::string... So asked myself: why in C++ the main() function is not overloaded to take an std::vector<std::string> argv instead of the old char* argv[]?

For "overload" I mean the coexistence of main() functions like int main() and int main(int argc, char *argv[]), not the overloading of a normal function made by the programmer.

解决方案

Why isn't it in the standard? Simple:

Because nobody proposed it.

Things go into the C++ standard because someone writes a proposal for it, then gets other people to vote on it and incorporate it. Unless someone actually wants it to happen, it doesn't get to happen.

And considering how trivial this feature really is:

int main(int argc, char **argv)
{
    std::vector<std::string> args(argv, argv + argc);
    ...
}

There just isn't any real need for it. It's a convenience function, and it doesn't even make things that convenient compared to the alternative.

这篇关于为什么C ++中的main()没有重载以使用std :: string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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