Can I use the first parameter as a default for the second parameter, like this?
int do_something(int a, int b = a + 1){
/* ... */
}
My compiler doesn't seem to like it, so perhaps it's not allowed.
Your question has already been answered (it is not allowed), but in case you didn't realize, the workaround is trivial with function overloading.
int do_something(int a, int b){
/* ... */
}
int do_something(int a) {
return do_something(a, a + 1);
}
It is not allowed.
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.
(from [dcl.fct.default]/9 in C++14)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With