Search code examples
c++overloadingdefault-arguments

C++ default arguments in function overload evaluated 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();

// is the default value of d recaculated in the function call?
z.f();

Solution

  • 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.