Comment on Problems with default function arguments (2020)parentComments−gpderetta2y void FooFunction(int x, int y, optional<int> optarg1 = {}, optional<int> optarg2 = {}) { int arg1 = optarg1.value_or(3); int arg2 = optarg1.value_or(7); } void usage_code() { FooFunction(x, y, {}, 9); } I.e. for complex interfaces defaulted arguments should default to an out-of-band placeholder, not to the actual value.I do like the struct as well, but it is still not ideal if you want to use initializers. I.e this doesn't work in C++: FooFunction(x, y, {.arg2 = 9}); You have to specify all preceding values FooFunction(x, y, {.arg1 = 2, .arg2 = 9});Works better with optional (and converting everything to a struct): FooFunction({.x = x, .y=x, .arg1 = nullopt, .arg2 = 9});
Comments
I do like the struct as well, but it is still not ideal if you want to use initializers. I.e this doesn't work in C++:
You have to specify all preceding values FooFunction(x, y, {.arg1 = 2, .arg2 = 9});Works better with optional (and converting everything to a struct):