I was wondering why C++ uses the following function prototype for the main() function:

int main(int argc, char *argv[]);

instead of something like:

int main(std::vector<std::string> argv);

What's the reasoning behind using the first? Why not just go with the (much more modern) version using vectors and strings? Is it for backwards compatibility, or is there something I'm missing?

  • 5
    Pretty much backwards compatibility, with the platforms which are used to calling a function with a signature of the first. – whatsisname May 14 '15 at 16:15
  • 2
    Already answererd stackoverflow.com/questions/12072600/… – Thomas Junk May 14 '15 at 16:17
  • 1
    Thats legacy. It's inherited directly from C, and it would break binary compatibility to change the prototype of main in the way you suggest. – cmaster - reinstate monica May 14 '15 at 16:41

A similar question has been asked on Stack Overflow.

In the accepted answer, Nicol Bolas points out that it isn't in the standard because it has to be proposed and voted on. In the way that C++ works, people write proposals for changes, which are voted on. However, he points out that it's a convenience function and doesn't add that much value over the current methods.

In another answer, iammilind suggests that it's against C++'s philosophy. This would add a dependency to the <string> and <vector> libraries. The current implementation uses built-in and independent types, which doesn't force the library on people. In addition, if a system didn't implement the library, then they would be unable to start the application.

Other answers are also provided, but the two mentioned above are the highest scoring.

  • 1
    It would also add a dependency on <vector>. – Gort the Robot May 14 '15 at 20:00
  • The kind of answer I was expecting. Clears it up, thanks. – InputUsername May 14 '15 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.