Avoid C++ implicit conversions from bool
Tags: cpp
Published: 2019-10-02 06:48:45

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](https://books.google.com.au/books?id=hS9mDwAAQBAJ&lpg=PT303&dq=%22design%20and%20evolution%20of%20c%2B%2B%22%20%22consider%20it%20absurd%20that%20anyone%20would%20bother%20to%20add%20such%20a%20type%20to%20C%2B%2B%22&pg=PT303#v=onepage&q=%22design%20and%20evolution%20of%20c++%22%20%22consider%20it%20absurd%20that%20anyone%20would%20bother%20to%20add%20such%20a%20type%20to%20C++%22&f=false), 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](https://books.google.com.au/books?id=hS9mDwAAQBAJ&lpg=PT303&dq=%22design%20and%20evolution%20of%20c%2B%2B%22%20%22most%20Boolean%20types%20were%20used%20in%20ways%20that%20required%20free%20conversion%20to%20and%20from%20int%22&pg=PT303#v=onepage&q=%22design%20and%20evolution%20of%20c++%22%20%22most%20Boolean%20types%20were%20used%20in%20ways%20that%20required%20free%20conversion%20to%20and%20from%20int%22&f=false). So in C++, `bool`-to-`int` conversion is implicit, with [`false` converting to 0 and `true` to 1](http://eel.is/c++draft/conv#prom-6).

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](https://twitter.com/nnethercote/status/1022422804937863168) 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](https://clang.llvm.org/extra/clang-tidy/checks/readability-implicit-bool-conversion.html), but by default it catches more conversions than just `bool` to `int`.

---

_Originally posted on [Tangent Spaces](https://tangentspaces.com/2018/09/22/avoid-cpp-implicit-conversions-from-bool/) -- I'm stealing my own content to try [dev.to](https://dev.to/), feedback welcome._ 😺