C++ Quiz59 of 173
Difficulty:
⬅️ Prev: 131Quiz ID: 132Next: 133 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

int foo() {
  std::cout << 1;
  return 1;
}

void bar(int i = foo()) {}

int main() {
  bar();
  bar();
}
Hint

When are default arguments evaluated?

Reveal answer

11

Explanation

Is foo called both times or just once? The C++ standard says this in §[dcl.fct.default]¶9:

A default argument is evaluated each time the function is called with no argument for the corresponding parameter.

Thus, foo is called twice.

⬅️ Prev: 131Quiz ID: 132Next: 133 ➡️