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++?
Because it will force you to include
<vector>
and<string>
.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?
Basically, to remain compatable with C. If we were to give up that, main() would be moved into a class.