Late answer, but I think it's the best. The real issue isn't compatibility with c or includes; it's the possibility of exceptions 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?
There's another reason besides compatibility with C. In C++, the standard library is meant to be entirely optional. There's nothing about the C++ language itself that forces you to use things from the standard library like std::string and std::vector , and that is entirely by design. In fact, it is by design that you should be able to use some parts of the standard library without having to use others (although this has led to some generally annoying things like std::ifstream and std::ofstream operating on const char* C-style strings rather than on std::string objects). The theory is that you are supposed to be able to take the C++ language and use whatever library of objects, containers, etc, that you want with it, be it the standard library or some proprietary library (e.g. Qt, MFC), or something that you created yourself. Defining main to accept an argument composed of types defined in the standard library defeats this design goal.
Because it will force you to include <vector> and <string>
| Like @jalf, I sometimes find myself writing int main(int argc, char** argv) {
std::vector<std::string> args(argv, argv+argc);
But yes, like everyone said, main has to be C-compatible. I see it as an interface to the OS runtime, which is (at least int the systems I use) is written in C. Although some development environment encourage replacements such as wmain or _tmain . You could write your own compiler/IDE, which would encourage the use of int vmain(const std::vector<std::string>& args) .
|
|
Because C++ was in existence long before the C++ standard was, and built heavily on C. And, like the original ANSI C standard, codifying existing practice was an important part of it.
There's no point in changing something that works, especially if it will break a whole lot of existing code.
Even ISO C, which has been through quite a few iterations, still takes backwards compatibility veryseriously.
|
|
answered
Aug 13 '10 at 15:00
| | |
feedback
|
|
Basically, to remain compatable with C. If we were to give up that, main() would be moved into a class.
|
answered
Aug 13 '10 at 14:57
| | |
feedback
|
|
I'll try explain in the best possible sentence.
C++ was designed to be backward compatible with C and std::vector was included in a library that only got included in C++.
Also, C++ and C programs were designed to run in shells or command lines (windows, linux, mac) and OS pass arguments to a program as an array of String. How would an OS really translate vectors?
That's the most reason I can think of, feel free to criticize it.
|
answered
Aug 13 '10 at 15:16
| | |
feedback
|
|
The multiple definitions of main() aren't really multiple definitions. There are three:
-
int main(void) (C99) -
int main(int argc, char *argv[]) (C99) -
int main(int argc, char *argv[], char *envp[]) (POSIX, I think)
But in POSIX, you only really get the third. The fact that you can call a function with extra arguments is down to the C calling convention.
You can't have extern "C" int main(std::vector<std::string> argv) unless the memory layout happens to be magically compatible in a portable way. The runtime will call main() with the wrong arguments and fail. There's no easy way around this.
Instead, provided main() wasn't extern "C" , the runtime could try the various supported symbols in order until it found one. I imagine main() is extern "C" by default, and that you can't overloadextern "C" functions.
For more fun, void main(void).
|
answered
Aug 13 '10 at 15:56
| | |
|
|
|