vsoftco vsoftco - 1 month ago 7
C++ Question

What is the reason C++ doesn't add `std::vector<std::string>` "overload" as argument to `main()`?

Is there any fundamental reason why the new C++17 (or later) won't allow for an alternative way of writing

main
as

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


? I know that one needs compatibility with previous code, so

int main(int, char**)


still has to exist, but is there anything technical that prevents the first "alternative" declaration?

Answer

This might be somewhat non-trivial to implement, at least in one respect.

This basically requires kind of a reverse-lookup form of function overloading. That is, the startup code normally looks roughly like this:

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

void entry() { 
    // OS-specific stuff to retrieve/parse command line, env, etc.
    static_constructors();
    main(argc, argv, envp);
    execute(onexit_list);
    static_destructors();    
}

With your scheme, we'd need two separate pieces of startup code: one that calls main passing argc/argv, the other passing a std::vector<std::string>.

I should add that while this means the job isn't entirely trivial, it's still far from an insurmountable problem. Just for one example, Microsoft's linker already links different startup code depending on whether you've defined main or WinMain (or wmain or wWinMain). As such, it's obviously possible to detect the (mangled) name of the entry point the user has provided, and link to an appropriate set of startup code accordingly.