#include <iostream>
#include <vector>
void f(std::vector<int> const &v, std::vector<int>::const_iterator it = v.end())
{
}
int main()
{
f({});
}
prog.cpp:4:73: error: local variable āvā may not appear in this context
void f(std::vector<int> const &v, std::vector<int>::const_iterator it = v.end())
^
Why is this not allowed? (I mean, what is the reasoning for defining the standard this way?)
Thankfully (hopefully) in C++14/C++17 we will have a unified way to represent end iterators without an instance of the container, but currently I just have to hope my implementation accepts a default-constructed iterator as an end iterator.
It would be possible in the language design, but it currently is not, because v is a local variable, and you may not reference local variables in default argument expressions.
The way to get around it is simply overloading the function, though frankly you are mixing things a little.
The order of evaluation of function arguments is unspecified. Consequently, parameters of a function shall not be used in a default argument, even if they are not evaluated. - IS
Ah, thanks, that makes much more sense now. I guess it would be extra work for the compiler to ensure that dependent arguments are evaluated before non-dependent ones.