简体   繁体   English

我可以设置上一个参数的默认参数吗?

[英]Can I set a default argument from a previous argument?

Is it possible to use previous arguments in a functions parameter list as the default value for later arguments in the parameter list? 是否可以将函数参数列表中的先前参数用作参数列表中后续参数的默认值? For instance, 例如,

void f( int a, int b = a, int c = b );

If this is possible, are there any rules of use? 如果可能,是否有任何使用规则?

The answer is no, you can't. 答案是否定的,你不能。 You could get the behaviour you want using overloads: 您可以使用重载获得所需的行为:

void f(int a, int b, int c);
inline void f(int a, int b) { f(a,b,b); }
inline void f(int a)        { f(a,a,a); }

As for the last question, C doesn't allow default parameters at all. 至于最后一个问题,C根本不允许使用默认参数。

No, that is not legal C++. 不,这不是合法的C ++。 This is specified in section 8.3.6/9 of the C++ Standard: 这在C ++标准的第8.3.6 / 9节中指定:

Default arguments are evaluated each time the function is called. 每次调用函数时都会评估默认参数。 The order of evaluation of function arguments is unspecified. 函数参数的求值顺序未指定。 Consequently, parameters of a function shall not be used in default argument expressions, even if they are not evaluated. 因此,即使未对函数的参数进行求值,也不得在默认参数表达式中使用该函数的参数。

and: 和:

int f(int a, int b = a); int f(int a,int b = a); // error: parameter a used as default argument //错误:参数a用作默认参数

And C89 at least does not support default parameter values. 而且C89至少不支持默认参数值。

As a potential workaround, you could do: 作为一种可能的解决方法,您可以执行以下操作:

const int defaultValue = -999; // or something similar

void f( int a, int b = defaultValue, int c = defaultValue )
{
    if (b == defaultValue) { b = a; }
    if (c == defaultValue) { c = b; }

    //...
}

这是不可能的

No, you cannot do that. 不,你不能那样做。
You will surely get an error "Local variable may not appear in this context". 您肯定会收到错误“本地变量可能不会在此上下文中出现”。

Your first idea might be to do something like this : 您的第一个想法可能是做这样的事情:

void something(int a, int b=-1, int c=-1){
    if(b == -1)
        b = a;
    if(c == -1)
        c = b;
}

I used -1 because this function only works with positive values. 我使用-1,因为此函数仅适用于正值。 But what if someone uses my class and makes a mistake which ends up sending -1 to the method? 但是,如果有人使用我的课程并犯了一个错误,最终向该方法发送-1,该怎么办? It would still compile and execute, but the result would be unpredictable for the user. 它仍然可以编译和执行,但是结果对于用户来说是不可预测的。 So the smart thing to do would be to remove any default argument and instead make a bunch of methods with the same name like this: 因此,明智的做法是删除所有默认参数,而是制作一堆具有相同名称的方法,如下所示:

void something(int a, int b, int c){
    /* Do something with a, b and c */
}

void something(int a){
    something(a, a, a);
}

void something(int a, int b){
    something(a, b, b);
}

It doesn't really take much longer to code, and if someone uses it in a programming interface with auto-complete features, it will show the 3 possible prototypes. 编码实际上并不需要花费更长的时间,如果有人在具有自动完成功能的编程界面中使用它,它将显示3种可能的原型。

I do not think you can do that as that is an illegal syntax. 我认为您不能这样做,因为那是非法语法。 But however, consult the C99 standard in pdf format ( n1136.pdf ). 但是,请查阅pdf格式的C99标准( n1136.pdf )。

However, you may get around this by using static as in declaring the variables static and using them within the function f 但是,您可以通过使用static来解决此问题,如声明变量static并在函数f使用它们

static int global_a;

/* In some other spot where you are calling f(), do this beforehand */
/* global_a = 4; f(); */

void f(void){
   int a = global_a;
   b = c = a;
   /* ..... */
}

Kudos to Michael Burr for pointing out my error! 感谢Michael Burr指出我的错误! :) :)

It sounds like you need to rethink your code and change it around for something like that. 听起来您需要重新考虑代码,并进行类似的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能使用以前的参数值来定义参数默认值? - Why I cannot use previous argument values to define argument default values? 我如何为 void 语句 C++ 中缺少的参数设置默认值 - How can i set a default value for a missing argument inside void statement C++ 我可以使用 clang AST 从模板专业化中获取默认模板参数吗? - Can I get default template argument from template specialization with clang AST? 为什么默认参数不能依赖于非默认参数? - why can't default argument depend on non-default argument? 为什么我不能在gcc的初始化程序中访问默认参数? - Why can't I access a default argument in its initializer in gcc? 我可以使用非默认数据类型作为1行中函数的参数吗? - Can I use non-default datatype as argument for functions in 1 line? 我可以在调用者处将函数参数默认为__FILE__的值吗? - Can I default a function argument to the value of __FILE__ at the caller? 我可以使用成员元素作为类方法的默认参数吗? - Can I use a member element as the default argument for a method of the class? 如何将其他类型的默认参数传递给参数? - How can I pass a default argument of other type to a parameter? 为什么我们不能从具有默认参数的函数调用函数? - Why can't we call a function from a function with a default argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM