Why is main () in C ++ not overloaded to use std :: string?

I tried to create a program that takes command line arguments using the arguments to the main() function. As a (main) C ++ programmer (even if I know C-style pointers and an array well), I almost never used char* strings or C-arrays. I spent a few to take main() arguments and convert them to std::string ... So I asked a question: why in C ++ the main() function is not overloaded to take std::vector<std::string> argv instead of the old char* argv[] ?

For overloading, I mean the coexistence of main() functions, such as int main() and int main(int argc, char *argv[]) , rather than overloading the normal function performed by the programmer.

+19
c ++ main command-line-arguments
Aug 22 '12 at 11:57
source share
5 answers

Why is this not a standard? Plain:

Because no one suggested this.

Everything goes to the C ++ standard, because someone writes a proposal for it, and then makes other people vote on it and turn it on. If someone really does not want this, this will not happen.

And considering how trivial this function is:

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

There simply is no real need. This is a convenient feature, and it does not even do what is convenient compared to the alternative.

+27
Aug 22 '12 at 12:23
source share

why in C ++ the main () function is not overloaded to take std :: vector argv instead of the old char * argv []

Because it requires a dependency on the <string> library . The C ++ philosophy is always "not paying for what you are not using." If someone does not want the automatic memory management offered by string , then they cannot be enforced.
2nd type: if there is no library support on any platform, you cannot start your C ++ program!

Conversely, the arguments int and char** are built-in and independent types. You can always write a custom wrapper in main() , which does exactly what you need.

Edit : comment on AProgrammer:
Assume main(vector<string>) allowed; then, if the platform meets all the features of C ++, but does not have standard library support, then it will become non-standard.

+17
Aug 22 2018-12-12T00:
source share

The main reason, I suspect, is that the usual implementation make it an extern "C" function with varargs. In many implementations, it is a C runtime library that calls main.

There are many ways to get around this, but none of them should be bothered, given how easy it is to convert arguments to whatever you want.

+4
Aug 22 2018-12-12T00:
source share

It would be trivial if the compiler should compile the main part, defined as

 int main(std::vector<std::string> args) { ... } 

since it was recorded as

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

It would even be easy to resolve any container, even custom one, and not just std::vector<std::string> . But this is not a standard, because - the standard answer to the question of why it is not in the standard - no one proposed it and convinced enough people that it was a good idea. I do not think there was a proposal, so there is probably no reason for his refusal. Perhaps this change is simple for most people involved.

+2
Aug 22 '12 at 12:23
source share

Basically, std :: vector does NOT match (in memory layout or something) as a char * array. To resolve this, you need to make the compiler recognize the new main declaration and add a wrapper to the record that created the row vector.

Given that you were doing this, you could also throw argc as well as declare the main

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

But it works for the compiler. You would need to get a lot of people who thought it was worth it.

Otherwise, you can just do it yourself

 int main(int argc, char **argv) { std::vector<std::string> args; args.reserve(argc); for (std::size_t arg = 0; arg < argc; ++arg) { args.push_back(argv[i]); } return mymain(args); } 

The code is not guaranteed to compile or work because I just wrote it from my head.

or (better, thanks to AProgrammer)

 int main(int argc, char **argv) { return mymain(std::vector<std::string>(argv, argv + argc)); } 
+1
Aug 22 '12 at 12:11
source share



All Articles