Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default parameters: can only the last argument(s) be left?

I know it's possible to do something like :

int foo(int a = 0, int b = 1) { return a + b; }

and then use it without the default parameters eg.:

foo();    // a = 0, b = 1 -> 1

or with the last one as default eg.:

foo(2);    // a = 2 and b = 1 default -> 3

But my question is: Is it possible to use the default value for the first argument (a) and give the value of the second (b)

My first thought was doing it like (which doesn't work!):

foo(,2);    // a = 0 default and b = 2

Does a syntax for this exist or is this just not possible ?

like image 749
UNKN57 Avatar asked Oct 03 '16 18:10

UNKN57


2 Answers

No, it is not possible in current syntax.

like image 57
SergeyA Avatar answered Sep 30 '22 15:09

SergeyA


Alternatively from specifying default parameter values you can use multiple function overloads like:

 int foo(int a, int b){return a+b; }
 int foo(int b){return foo(0,b); }     
 int foo(){return foo(0,1); }
like image 24
πάντα ῥεῖ Avatar answered Sep 30 '22 14:09

πάντα ῥεῖ