Skip to content

Commit edffb8c

Browse files
committed
Make operator bool() explicit
This prevents unwanted conversions to bool or int such as: ``` py::object my_object; std::cout << my_object << std::endl; // compiles and prints 0 or 1 int n = my_object; // compiles and is nonsense ``` With `explicit operator bool()` the above cases become compiler errors.
1 parent 135fd14 commit edffb8c

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

include/pybind11/pytypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class handle : public detail::object_api<handle> {
8888
const handle& dec_ref() const { Py_XDECREF(m_ptr); return *this; }
8989

9090
template <typename T> T cast() const;
91-
operator bool() const { return m_ptr != nullptr; }
91+
explicit operator bool() const { return m_ptr != nullptr; }
9292
bool operator==(const handle &h) const { return m_ptr == h.m_ptr; }
9393
bool operator!=(const handle &h) const { return m_ptr != h.m_ptr; }
9494
bool check() const { return m_ptr != nullptr; }

0 commit comments

Comments
 (0)