In the code above, the following is a class (struct) non static data member hour:
vector<double> hour {vector<double>(24,not_a_reading)};
It has a default member initializer: {vector<double>(24,not_a_reading)}
But is there any reason why the author's initialization technique is superior to just doing:
vector<double> hour(24, not_a_reading)
Yes, you would not be able to write the initializer of a class member that way. You need the curly braces in the class (struct) definition to make it an initializer or you could use the syntax: vector<double> hour = vector<double>(24,not_a_reading); which would mean the same thing.
#include <vector> using namespace std; int main() { const int not_a_reading = -7777; struct Day { vector<double> hour{vector<double>(24,not_a_reading)}; // create a vector of doubles object with the constructor and then initialize hour with 24 doubles vector<double> hour2 = vector<double>(24,not_a_reading); // same as above }; //struct Day2 { // vector<double> hour(24,not_a_reading); // syntax error //}; struct Day3 { vector<double> hour(int,int); // function declaration! }; vector<double> other_hour(24,not_a_reading); // ok here vector<double> other_hour2(); // function declaration, most vexing parse! vector<double> another_hour{vector<double>(24,not_a_reading)}; // also ok here return 0; }
A possible reason that vector<double> hour(24,not_a_reading); is not allowed to create an hour object is because under certain circumstances it could be confused with a function declaration. The so called most vexing parse.