Found this by accident where a function declaration and definition may not agree on the constness of parameters. I've found some information (links following), but my question is why is const matching optional for by-value parameters, but const matching is required for reference parameters?
Consider the following code available here.
class MyClass
{
int X;
int Y;
int Z;
public:
void DoSomething(int z, int y, const int& x);
int SomethingElse(const int x);
void Another(int& x);
void YetAnother(const int& z);
};
void MyClass::DoSomething(int z, const int y, const int& x) // const added on 2nd param
{
Z = z;
Y = y;
X = x;
}
int MyClass::SomethingElse(int x) // const removed from param
{
X = x;
x = 3;
return x;
}
void MyClass::Another(int& x) // const not allowed on param
{
X = x;
}
void MyClass::YetAnother(const int& z) // const required on param
{
Z = z;
}
I've found this on SO, but it is looking for explanation for name mangling. I've also found this on SO and this on SO, but they don't go into detail on why const matching is required for reference parameters.