Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I bet on Bjarne Stroustrup being too busy setting other programmers computers on fire before coming to mine.

More seriously, the typical response to undefined behavior is for the compiler to optimize out whatever code may trigger it. It is great for performance and one of the reasons C and C++ often top the charts, but it may lead to weird bugs that depend on the optimization level.

For the union case, the compiler could completely remove the code that reads the float: because it is UB, it is something you shouldn't do, so it considers that you will never do it, so it can just remove the code. In the end your conversion function may do nothing at all (very optimized!). In practice, because it is so common, even though it is UB, it will usually do what you want it to do.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: