In C and C++, parameters can be declared const
when defining a function:
// without const
void foo(int i)
{
// implementation of foo()
}
// with const
// const communicates the fact that the implementation of foo() does not modify parameter i
void foo(const int i)
{
// implementation of foo()
}
From the caller's perspective, though, both versions are identical (have the same signature). The value of the argument passed to foo()
is copied. foo()
might change that copy, not the value passed by the caller.
This is all well, but one is allowed to use const
also when declaring foo()
:
void foo(int i); // normal way to declare foo()
void foo(const int i); // unusual way to declare foo() - const here has no meaning
Why is const
allowed by the C and C++ standards when declaring foo()
, even though it is meaningless in the context of such a declaration? Why allow such a nonsensical use of const
?