Why is argc not a constant?

后端 未结 7 552
时光说笑
时光说笑 2020-12-07 21:58
int main( const int argc , const char[] const argv)

As Effective C++ Item#3 states \"Use const whenever possible\", I start thinking \"why not make

相关标签:
7条回答
  • 2020-12-07 22:29

    A top level const on a formal argument is not part of the function type. You can add it or remove it as you like: it only affects what you can do with the argument in the function implementation.

    So for argc you can add freely add a const.

    But for argv you can not make the character data const without thereby changing the function signature. Which means that it's then not one of the standard main function signatures, and will not have to be recognized as a main function. So, not a good idea.


    A good reason for not using the standard main arguments in non-toy programs is that in Windows they are not able to represent actual program arguments such as filenames with international characters. That's because in Windows they are by very strong convention encoded as Windows ANSI. In Windows you can implement some more portable argument access facility in terms of the GetCommandLine API function.


    Summing up, nothing prevents you from adding const to argc, but the most useful const-ness on argv would give you a non-standard main function, most probably not recognized as such. Happily (in an ironic way) there are good reason to not use the standard main arguments for portable serious code. Quite simply, for the in-practice they only support old ASCII, with only English alphabet letters.

    0 讨论(0)
提交回复
热议问题