Why isn't main defined `main(std::vector

2019-04-19 03:29发布

This question is only half tongue-in-cheek. I sometimes dream of a world without naked arrays or c strings.

If you're using c++, shouldn't the preferred definition of main be something like:

int main(std::vector<std::string> args)

?

There are already multiple definitions of main to choose from, why isn't there a version that is in the spirit of C++?

9条回答
Summer. ? 凉城
2楼-- · 2019-04-19 04:01

Because it will force you to include <vector> and <string>.

查看更多
冷血范
3楼-- · 2019-04-19 04:06

Because C++ was designed to be (almost) backwards compatible with C code.

There are cases where C code will break in a C++ compiler, but they're fairly rare, and there's generally a good reason for why this breakage is required.

But changing the signature of main, while convenient for us, isn't necessary. For someone porting code from C, it'd just be another thing you had to change, for no particular gain.

Another reason is that std::vector is a library, not a part of the core language. And so, you'd have to #include <vector> in every C++ program.

And of course, in its early years, C++ didn't have a vector. So when the vector was added to the language, sure, they could have changed the signature of main, but then they'd break not just C code, but also every existing C++ program.

Is it worth it?

查看更多
Explosion°爆炸
4楼-- · 2019-04-19 04:06

Basically, to remain compatable with C. If we were to give up that, main() would be moved into a class.

查看更多
登录 后发表回答