In this case, history is a factor. C defined these inputs as "not constant", and compatibility with (a good portion of) existing C code was an early goal of C++.
Some UNIX APIs, such as getopt
, actually do manipulate argv[]
, so it can't be made const
for that reason also.
(Aside: Interestingly, although getopt
's prototype suggests it won't modify argv[]
but may modify the strings pointed to, the Linux man page indicates that getopt
permutes its arguments, and it appears they know they're being naughty. The man page at the Open Group does not mention this permutation.)
Putting const
on argc
and argv
wouldn't buy much, and it would invalidate some old-school programming practices, such as:
// print out all the arguments:
while (--argc)
std::cout << *++argv << std::endl;
I've written such programs in C, and I know I'm not alone. I copied the example from somewhere.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…