c ++默认参数:只能留下最后一个参数吗?

UNK*_*N57 4 c++ default-parameters

我知道可以这样做:

int foo(int a=0, int b=1){return a+b}
Run Code Online (Sandbox Code Playgroud)

然后在没有默认参数的情况下使用它,例如:

 foo(2,3)
Run Code Online (Sandbox Code Playgroud)

或者使用最后一个作为默认值,例如:

foo(2) // a=2 and b=1 default 
Run Code Online (Sandbox Code Playgroud)

我的问题是:是否可以使用第一个参数(a)的默认值并给出第二个参数的值(b)

我的第一个想法是这样做:

foo(,2) // a=0 default and b=2
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

是否存在这种语法或者这是不可能的?

πάν*_*ῥεῖ 8

或者,除了指定默认参数值之外,您还可以使用多个函数重载,例如:

 int foo(int a, int b){return a+b; }
 int foo(int b){return foo(0,b); }     
 int foo(){return foo(0,1); }
Run Code Online (Sandbox Code Playgroud)


Ser*_*eyA 7

不,在当前语法中是不可能的.