开发者

C++ default arguments in function overload evaluated once or each time?

开发者 https://www.devze.com 2023-02-26 06:15 出处:网络
I have a question on defaults for overload functions in an object. If I have a function signature as follows will the default value be evaluated only once or each time?

I have a question on defaults for overload functions in an object.

If I have a function signature as follows will the default value be evaluated only once or each time?

class X
{
  public:
  f(const RWDate& d=RWDate::now());
}

// when calling f() do I get the current time each time?
X z;
z.f();

// 开发者_Go百科is the default value of d recaculated in the function call?
z.f();


The default arguments are substituted at the call site, so z.f() is transformed into

z.f(RWDate::now())

Thus, the default argument is evaluated each time the function is called and the default argument is used.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号