假设我有:
void f(bool option1 = false, bool option2 =false, bool option3 = false)
{
...
}
我想调用:
f(option2=true);
这在 C++ 中可能吗?
最佳答案
不可能按照您在 C++ 中建议的方式调用函数。您可以通过元编程模拟命名参数,或者简单地将struct
传递给您的函数。例如
struct options
{
bool option0{false};
bool option1{false};
bool option2{false};
};
void f(options opts = {});
C++11 用法:
options opts;
opts.option2 = true;
f(opts);
C++2a 用法:
f({.option2=true});
关于C++ 默认参数乱序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50357982/