Ali.S Ali.S - 1 year ago 134
C++ Question

Using ternary operator to initialize a reference variable?

Putting all the maintainability and reading issues aside, can these lines of code generate undefined behavior?

float a = 0, b = 0;
float& x = some_condition()? a : b;
x = 5;
cout << a << ", " << b;

Answer Source

No, it's just fine. It would not create undefined behavior in this code. You will just change value of a or b to 5, according to condition.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download