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.
argc
asconst
. – Nakesha--argc
– Hoeveconst
; indeed, passingargc
as aconst int
means that you can't then useargc
as, say, a counter inside the function. – Acescentconst
a pass-by-value parameter. See eg https://mcmap.net/q/205041/-isn-39-t-quot-const-quot-redundant-when-passing-by-value-duplicate and https://mcmap.net/q/47099/-does-using-const-on-function-parameters-have-any-effect-why-does-it-not-affect-the-function-signature – Andimain
's function signature is unchanged since the days of Unix 7, and there was noconst
back then (it was a shiny new feature of C89). – Dutiful