Reputation: 177
Are there benefits to instantiating structs using aggregate initialization as opposed to having an init function to return an instance? This is what I'm talking about:
sturct Data
{
float x, y;
std::string dataString;
};
Data init_data(float _x, float _y, std::string _dataString)
{
Data data;
data.x = _x;
data.y = _y;
data.dataString = _dataString;
return data;
}
int main()
{
Data d1 = init_data(1.0, 2.0, "testData"); // or
Data d2 { 1.0, 2.0, "testData");
}
Upvotes: 0
Views: 78
Reputation: 1129
In the sense of performance, probably yes. d2 directly generates the Data instance with specified data values, while d1 is generating the a Data{0,0,empty string} first inside the function, and then modifies the values that you wanted, then return that Data by value, making another copy of Data. although the optimization the compiler offers may reduce the overhead, but it is not guaranteed.
Upvotes: 1