I thought I understood why C++ won't allow a local variable as a default function parameter:
int main () {
auto local{1024};
auto lambda = [](auto arg1 = local){}; // "illegal use of local variable as default parameter"
}
but it's not allowed even when that variable is a constexpr local:
int main () {
constexpr auto local{1024};
auto lambda = [](auto arg1 = local){}; // "illegal use of local variable as default parameter"
}
however, a global variable (even if non-constexpr) is allowed:
int global;
int main () {
auto lambda = [](int arg1 = global){}; // OK
}
Can someone explain the rationale for not allowing a constexpr local variable in this situation? It would seem the compiler should be able to construct the appropriate "defaulted argument" overloads for a function when that default value is fixed and known at compile-time.