The weblog of Nicholas Chapman Things that suck about C++ #1Posted 12 Apr 2016I found a nasty bug today in some of my code. Let me explain. First, pop quiz: What is the value of this C++ expression: 10.0f + true ? 1.0f : 2.0f;  If you answered 11.f, you're wrong. The actual answer is 1.0f. The ternary conditional operator (?:) has very low precedence (See http://en.cppreference.com/w/cpp/language/operator_precedence, it's right down in box 15). The plus operator has higher precedence, so the expression gets evaluated as (10.0f + true) ? 1.0f : 2.0f;  This code, which is a gross violation of any reasonable type system, compiles without warning! You can add 10.0f to a boolean value apparently and it's just fine! Of course the reason is that true gets converted to the integer 1, for historical C reasons presumably. And then the resulting value of 11.f effectively gets converted back to a boolean! (to evaluate the condition) The thing that sucks about C++ in this case is the automatic conversion of true to 1, which allows 10.0f + true to pass the type checker. Automatic conversion/casting of types in a language is very dangerous. Each case should be closely considered by the designer(s). And in this case it's just a bad idea. Fuck you C++. Edit: This is how our programming language Winter handles it: AdditionExpression: Binary operator '+' not defined for types 'float' and 'bool' buffer, line 1: def main(real x) real : 10.0f + true ? 1.0f : 2.0f ^  Much better.Do you have a comment or feedback about this blog post? Please email me.< Back All content by Nicholas Chapman. Static Pages on this website Our software Indigo Renderer Chaotica