Why are these default arguments allowed?

后端 未结 3 1630
面向向阳花
面向向阳花 2021-01-19 03:14

I\'ve found this question, and I\'m completely baffled.

The answer says b is invalid, \"Non-static members can not be used as default arguments.\". That

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 03:57

    Default arguments aren't evaluated at compile time. They're evaluated each time a function call requires them. They can be any expression that's valid in the context of the function declaration and has a suitable type, except that it can't use any other parameters (including this, in the case of a member function).

    That restriction is because the evaluation order isn't specified, so there's no way to ensure a parameter value is available if it were needed to evaluate another argument.

提交回复
热议问题