Can I set a default argument from a previous argument?

后端 未结 7 1751
终归单人心
终归单人心 2020-11-27 20:09

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(         


        
相关标签:
7条回答
  • 2020-11-27 20:43

    No, that is not legal C++. This is specified in section 8.3.6/9 of the C++ Standard:

    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); // error: parameter a used as default argument

    And C89 at least does not support default parameter values.

    0 讨论(0)
提交回复
热议问题