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();
}When are default arguments evaluated?
11
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.