Evaluation_order#2

PHOTO EMBED

Fri Mar 11 2022 09:14:20 GMT+0000 (Coordinated Universal Time)

Saved by @gtsekas #c++

#include <iostream>
#include <vector>

int f() { std::cout << "f"; return 0;}
int g() { std::cout << "g"; return 0;}

void h(std::vector<int> v) {}

int main() {
    h({f(), g()});
}
content_copyCOPY

The goal of this question is to demonstrate that the evaluation order of elements in an initializer list is specified (as opposed to the arguments to a function call). §[dcl.init.list]¶4: Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions, are evaluated in the order in which they appear. If h took two ints instead of a vector<int>, and was called like this: h(f(), g()); the program would be unspecified, and could either print fg or gf.

https://cppquiz.org/