c++ - 多个默认可选值

标签 c++ parameter-passing optional-parameters default-parameters

<分区>

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

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+options DoSomethingS。

关于c++ - 多个默认可选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54616059/

相关文章:

c++ - 来自用作模板参数的指针的类类型

c++ - Qt的foreach表达式需要深拷贝?

c++ - 当默认参数已经定义时,调用不带参数的函数

c# - 如何传递可选参数

c++ - 对 C++ 字符串使用 getline 以包含空格

c++ - Allegro 程序显示黑屏然后崩溃

ios - 如何在 swift : 中创建带有参数的单例

javascript - 在 JavaScript 中作为参数而不是字符串传递的数组的意外行为

F# 将字符串传递给列表

javascript - 具有默认值的选项的 javascript 设计模式?