You have quite a lot of flexibility. value2
needs to be an expression which is valid at the point of declaration of the function (by "valid" I mean that the names it uses are in scope, etc), and its type must be implicitly convertible to Type2
, same as for any initializer. value2
is evaluated each time the function is called. So for example:
#include <vector>
#include <iostream>
int i;
void foo(std::vector<int> v = std::vector<int>(i)) {
std::cout << v.size() << "\n";
}
int main() {
i = 1;
foo();
i = 2;
foo();
}
With the right initializer, Type2
can even be a reference type:
void bar(int &j = i);