Using ternary operator to initialize a reference variable?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 23:25:10

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.

This is absolutely fine, as long as both sides of the conditional are expressions that can be used to initialize a reference (e.g. variables, pointer dereferences, etc)

float& x = some_condition()? a : *(&b); // This is OK - it is the same as your code
float& x = some_condition()? a : b+1;   // This will not compile, because you cannot take reference of b+1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!