DEV Community

Cover image for Avoid C++ implicit conversions from bool
Gerald Squelart
Gerald Squelart

Posted on

Avoid C++ implicit conversions from bool

To most C++ programmers — or at least those like me with a short memory — it may feel like bool has always been there. However it is still not present in its ancestor C (though C99 has _Bool), and it was only introduced in C++98. At that time, there was resistance to it, and examination of existing code (that was mostly using macros to simulate a boolean type) showed that preventing implicit conversion between boolean values and integer numbers would have broken a lot of things. So in C++, bool-to-int conversion is implicit, with false converting to 0 and true to 1.

I think that this implicit conversion from bool is generally not needed in “real-world” code, and that it is in fact a potential source of bugs.

For example, this tweet shows that because of this implicit conversion, a typo like in int x = 1 < y; (where < was meant to be the left-shift operator <<) didn’t raise any alarm.

In my coding experience, I cannot think of useful cases where I would need to implicitly convert a bool to something else, apart from printf statements used during debugging — but for these, I have taken the habit of doing an explicit conversion anyway, e.g.: printf("b=%d", int(b));.

I can certainly imagine cases where a bool could be converted to 0 or 1, to be used in a mathematical expression, e.g.: a = useC * c, but I think this would be more clearly expressed as a = useC ? c : 0.

Of course my experience is limited, and there may be genuine situations where this conversion could genuinely help — Please let me know. But I think that in these cases, making the conversion explicit should be trivial, and also useful by making it more obvious (a = int(useC) * c). Now, if such conversion is needed for a lot of your code, maybe it’s a sign that the bool type may not be the right choice.

Note: clang-tidy offers readability-implicit-bool-conversion, but by default it catches more conversions than just bool to int.


Originally posted on Tangent Spaces -- I'm stealing my own content to try dev.to, feedback welcome. 😺

Top comments (2)

Collapse
 
sdryds profile image
Stewart Smith • Edited

I would say that in the modern C++ paradigm, implicit conversions are generally considered to be bad. That's why there is a big push for explicit single argument constructors. I think you've stumbled upon one if the many oddities of the language that have been grandfathered in by necessity rather than chosen willingly. It's the need for backwards compatiblity that has driven so many seemly insane decisions.

Personally I try to provide a type for every data concept in my program. I use the type_safe strong_types library instead of type aliasing for anything that would be a built in type. That way I get no implicit conversions and bring in additional checks from the type system.

Collapse
 
bjauny profile image
Bastien JAUNY

Having worked on applying MISRA compliance to legacy code in my early years, it's now almost hardwired in my brain to do explicit conversions, just for the sake of clarity.
Of course it's overkill in some cases but I usually see my future self looking at the code and be "What's going on here?" and being explicit is rarely too much.
Nice history, instructive post. :)