Why doesn't C ++ allow a parameter to be the default argument?

void g(int n, decltype(n) = 0); // ok
void f(int n, int = n); // error : default argument references parameter 'n'

int main()
{
    f(1); // want it to be same as f(1, 1);
}

      

Why doesn't C ++ allow a parameter to be the default argument?

What is the rationale?

+3


source to share


1 answer


One frequently cited potential justification for this limitation is that allowing parameters as default arguments would require imposing at least partial ordering on the parameter estimates. Parameters that are used as default arguments in other parameters must be evaluated first.

Meanwhile, C ++ continues to follow the original approach to parameter estimation: parameters are evaluated in an unspecified order.



The same reasoning can be used to explain why class members cannot be referenced in the default arguments for member functions: this imposes some ordering requirements when evaluating a hidden parameter this

.

+10


source







All Articles