bindings: cxx: make operator bool() explicit

A non-explicit operator bool() is dangerous: It allows unwanted
conversion to integral types:

gpiod::chip c;
int i = c;

This is a trivial example, but more insidious effects are possible,
e.g. with std::set<gpiod::chip>, which would use the operator bool()
to implement element comparison, thus turning such set into a set that
has at most 2 elements: An invalid (false) element and just 1 valid
element:

std::set<gpiod::chip> s;
s.emplace();
s.emplace("/dev/gpiochip0");
s.emplace("/dev/gpiochip1");
// s.size() is still 2 even if both chips were opened!

Making the operator explicit disables this.

See e.g.
https://en.cppreference.com/w/cpp/language/implicit_conversion#The_safe_bool_problem
for more info on this.

Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
2 files changed