多个默认可选值
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。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如何使用默认参数等选择模板专业化
- 具有默认模板参数的多态类的模板推导失败
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 初始化具有非默认构造函数的std::数组项的更好方法
- 何时提供默认参数作为模板参数
- 是默认情况下分配给char数组常量的值
- 具有默认值的引用获取函数
- 具有默认模板类型的默认构造函数的类型推导
- 当给定默认值时,为什么此模板参数推导失败
- 修改 VS Code 中的默认C++代码段
- 声明默认的模板化函数
- 将const引用参数初始化为默认参数会导致悬空引用吗
- 如何使用非默认构造函数实例化模板化类
- 如何修复带有 clang 的参数'args'缺少默认参数的问题?
- 从具有默认值的部分指定模板类继承时发生SWIG错误,具有不带默认值的正向声明
- 为什么大多数 pair 实现默认不使用压缩(空基优化)?
- 格式化浮点值:返回默认值
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr