问题
The valid C++ main
signatures are the following:
int main()
int main(int argc, char *argv[])
int main(int argc, char **argv)
But isn't allowed to declare main
taking an initializer list:
int main(std::initializer_list<char *> args)
AFAIK the initializer list could be implemented as a pair of pointers or a pointer (this could be the argv
parameter) plus a length (this could be deduced from the argc
parameter), and its storage could be automatic, temporary, or static read-only memory depending on the situation.
So i think that an std::initializer_list<char *>
could handle and manage without any problem the command line parameters, then i'm wondering why this hypothetical main
signature wasn't added after the approval of the initializer lists on the C++11 standard, and because of that i'm asking:
- What could be the disadvantages or problems derived of allowing an initializer list as the
main
only parameter? (i couldn't think of any). - Which would be the correct way to propose to the standards committee this addition (or any other change)?
回答1:
Although there are two ways of specifying main()
in a program, most (all?) current implementations of the C++ run-time call the main()
function the same way (they pass the arguments as (int, char *[])
irrespective to how main()
is declared). Your proposal would require the C++ run-time of all implementations to figure out which kind of main()
the program is using, and call the right main()
. If you really want this functionality for yourself, you can always provide an implementation of main(int, char *[])
that converts the arguments into an initializer list like object (such as vector<>
), and then calls a new entry point function of your choice.
The process for submitting a proposal is described at the Standard C++ website. The basic steps are: (1) float the idea on their Usenet group/mailing list; (2) draft a proposal, solicit feedback, and update the proposal accordingly; and (3) repeat the process until the proposal is accepted.
回答2:
It is worth noting that a library (but not the standard C++ library according to 3.6.1 [basic.start.main] paragraph 2, first sentence: "An implementation shall not predefine the main
function.") could just define function main()
and implement it in a suitable way, e.g., calling app_main()
:
#include <initializer_list>
#include <string>
#include <vector>
extern int app_main(std::vector<std::string> const&);
int main(int ac, char* av[]) {
return app_main(std::vector<std::string>(av, av + ac));
}
Since main()
can't be overloaded (according to 3.6.1 [basic.start.main] paragraph 2, second sentence: "This function shall not be overloaded.") the entry point needs a different name. Since std::initializer_list<T>
cannot be constructed other than using { ... }
, a different type than std::initializer_list<T>
needs to be used. While being at it, the above implementation also chooses to turn the char*
into std::string
objects.
To use the above function, an application would just implement app_main()
and it would get called:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
int app_main(std::vector<std::string> const& args) {
std::cout << "received the arguments: ";
std::copy(args.begin(), args.end(),
std::ostream_iterator<std::string>(std::cout, " "));
return EXIT_SUCCESS; // return statement can't be omitted, of course
}
回答3:
I think it is due to the considerations of the C compatibility and not the technical reasons. There are a lot stuff inherited from C.
来源:https://stackoverflow.com/questions/19099615/why-initializer-list-cannot-be-mains-parameter-how-to-propose-it