多个默认可选值

multiple default optional values

本文关键字:默认      更新时间:2023-10-16

我有一个接受两个默认值整数的函数,有没有办法允许函数的调用者传递任意数量的参数?(第一个但不是第二个,第二个但不是第一个,两者都有(。例:

void do_something(int first = 0, int second = 0);
int main()
{
   do_something(1); // first - how to declare it's the first argument
   do_something(1); // second
   do_something(1,1); 
   do_something();
   return 0; // I want to allow all those options
}

使用另一个包装器内联函数

   void DoSomething(int f = 0 , int s = 0)
   {}
   void inline DoSomethingS(int s = 0)
   {DoSomething(0 , s);}

如果用户想要发送第二个并保留第一个可选,他将使用DoSomething+选项,在这种情况下为DoSomethingS。