0

I'm using C++/CLR .NET, and defining a main.

In C, I would use the normal

int main(int argc, char* argv[]) //Typical for C, works for C++

However, being .NET I've also seen

int main(array<System::String ^> ^args) //Managed .NET array type 

How far can I take this? Can I place the command line arguments into nearly any structure I feel like? For example...

 int main(int argc, string argv[]) //More useful if I want strings anyway
 int main(vector<string> args)
davidhood2
  • 1,367
  • 17
  • 47
  • No. You can only use the forms the hosting environment explicitly supports. – StoryTeller - Unslander Monica Nov 16 '16 at 20:07
  • Is there any information on where/what these are? – davidhood2 Nov 16 '16 at 20:08
  • Yes, see [here](http://en.cppreference.com/w/cpp/language/main_function). – Captain Obvlious Nov 16 '16 at 20:09
  • [This link](https://msdn.microsoft.com/en-us/library/88w63h9k.aspx) – StoryTeller - Unslander Monica Nov 16 '16 at 20:10
  • @CaptainObvlious it doesn't say `main` can be defined however *we* want. Only that a hosting environment *may* provide other options – StoryTeller - Unslander Monica Nov 16 '16 at 20:11
  • As an additional, have you tried making a simple program using `int main(int argc, string argv[])` or `int main(vector args)` as the `main` function and see what the results are? – txtechhelp Nov 16 '16 at 20:11
  • Technically, `int main(int argc, std::vector argv) { for (int i = 0; i < argc; ++i) { std::cout << argv[i] << std::endl; } return 0; }` compiles and runs, but it doesn't produce what you think it would ... – txtechhelp Nov 16 '16 at 20:20
  • @txtechhelp Have tried both my examples, and yours - they all compile and then throw an error to do with objects not being initialised. I'm guessing that would be VS15 way of saying it cant be defined that way – davidhood2 Nov 16 '16 at 20:44
  • @davidhood2 .. the objects aren't initialized because they're essentially being casted from `char*[]` to `std::string[]` which is undefined behavior. That code will compile on just about any C++ compiler (`gcc` as well). The `main` function has a specific definition that the compiler builds out that the kernel can then call when it needs to execute, thus you're forcing the `main` function to do something that is not standard. – txtechhelp Nov 17 '16 at 00:57

0 Answers0