Reputation: 5666
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.
Upvotes: 4
Views: 757
Reputation: 103693
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);
}
Upvotes: 12
Reputation: 1
if I were U I would do this
int do_something(int a, int b){
b = a + 1;
/* ... */
}
But the b value is not gonna change after the function complete executing. Passing b by reference will work. (int &b)
Upvotes: -5
Reputation: 119089
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)
Upvotes: 9