I spotted a "mistake" in my code today... But I'm not sure if the "mistake" actually changes the resulting compiled code.
Consider initialization of the following reference to a double
, x
.
double &x{*_data->x_double}; // x_double is a member variable of the struct
// _data, it is a pointer to another double
// eg, I have this somewhere else...
struct data
{
double *x_double;
};
data *_data = new data; // edit: duh, this has to be a pointer...
double another_x = 10.0;
_data->x_double = &another_x; // edit: pointer here too!
However I made the following "mistake"... (notice the extra =
sign)
double &x={*_data->x_double};
This code is a minimal example copied from my actual code, in which I don't have references to doubles, but references to large objects such as std::vector
's.
The reason for the reference variables is that they are used in an algorithm, and the variable names are very long, so I create shorted aliases for those variables using references. Hope it makes sense why I did that...
So, I've "corrected" the "mistake" but has my output compiled code actually changed?
They're both list initialization . For your cases:
double &x={*_data->x_double};
Is :
6) initialization of a named variable with a braced-init-list after an equals sign
And
double &x{*_data->x_double};
Is :
1) initialization of a named variable with a braced-init-list (that is, a possibly empty brace-enclosed list of expressions or nested braced-init-lists)
Their effects are in this case exactly the same.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.