Why is argc not a constant?

后端 未结 7 559
时光说笑
时光说笑 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:19

    Aside from the historical reasons, a good reason to keep argc and argv non-const is that the compiler implementation doesn't know what you're going to do with the arguments to main, it just knows that it must give you those arguments.

    When you are defining your own functions and associated prototypes, you know which parameters you can make const and which ones your function will modify.

    Taken to an extreme, you could declare that all parameters to all functions should be declared const, and then if you had a reason to change them (e.g. decrement an index to search through an array), you'd have to make local non-const variables and copy the const arguments' values into those variables. That makes for busy-work and extra LOC with no real benefit. A decent static analyzer will pick up if you're not modifying the value of an argument, and recommend that you make the parameter const.

提交回复
热议问题