Discussion: Off to a Good Start
Execute the code to understand the output and gain insights into function argument evaluation order and its implications.
We'll cover the following...
Run the code
Now, it’s time to execute the code and observe the output.
#include <iostream>struct Logger {};struct Configuration {};Logger initializeLogger(){std::cout << "Initializing logger\n";return Logger{};}Configuration readConfiguration(){std::cout << "Reading configuration\n";return Configuration{};}void startProgram(Logger logger, Configuration configuration){std::cout << "Starting program\n";}int main(){startProgram(initializeLogger(), readConfiguration());}
Understanding the output
In this puzzle, we want to start a program that relies on a logger having been set up and a configuration having been read. As long as both of those are done before the program starts, we’re good. The order in which the logger is set up and the configuration is read doesn’t seem to matter here, but what if it did? What if, for instance, initializing the logger sets up some global logger instance that we use while reading the configuration? Can we rely on the two functions being called in the order listed in the program? Unfortunately, we can’t since the evaluation order of function argument expressions is unspecified. ...