Re:Named Booleans prevent C++ bugs and save you time
by lazypenguin on Saturday February 18, 2023 @08:02AM
(#34846593)
My mistake for reading HN too early in the morning. You and the other poster are right.
Re:Named Booleans prevent C++ bugs and save you time
by ninkendo on Saturday February 18, 2023 @08:08AM
(#34846642)
(Fun fact: I read meindnoch's comment right after I woke up and I, too, was about to reply to them with a godbolt listing to show that it would compile to the same thing, only to realize I completely forgot about short-circuit semantics. So you're not alone!)
The ability of the compiler to skip the expensive function in both cases are characteristics of a "lazy" language, ie. you can assign an expensive function to a value, but the actual function isn't run until the variable is needed. Languages like C++ can't do this because it would alter the program's behavior if cheapCheck() does return true, because suddenly it would have to call expensiveCheck() after cheapCheck() (rather than before), which is an observably different behavior for the optimization to cause.