跳过 C++ 函数中的一些参数? [英] Skip some arguments in a C++ function?

查看:45
本文介绍了跳过 C++ 函数中的一些参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 函数,它有 5 个参数,所有参数都有默认值.如果我传入前三个参数,程序将为后两个参数分配一个默认值.有没有办法传递3个参数,然后跳过中间的一个,给出值来表示第一个、第二个和第五个参数?

I have a C++ function that has 5 arguments, all of which have default values. If I pass in the first three arguments, the program will assign a default value to the last two arguments. Is there any way to pass 3 arguments, and skip one in the middle, giving values to say, the first, second, and fifth arguments?

推荐答案

不是直接的,但是你可以用 std::bind 做一些事情:

Not directly, but you might be able to do something with std::bind:

int func(int arg1 = 0, int arg2 = 0, int arg3 = 0);

// elsewhere...
using std::bind;
using std::placeholders::_1;
auto f = bind(func, 0, _1, 0);

int result = f(3); // Call func(0, 3, 0);

缺点当然是您要重新指定默认参数.我相信其他人会提出更聪明的解决方案,但如果您真的很绝望,这可能会奏效.

The downside is of course that you are re-specifying the default parameters. I'm sure somebody else will come along with a more clever solution, but this could work if you're really desperate.

这篇关于跳过 C++ 函数中的一些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆