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

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

问题描述

我有一个具有5个参数,都具有默认值的C ++函数。如果我首次通过三个参数,程序将默认值分配给最后两个参数。有什么办法来传递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 ::绑定:

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天全站免登陆