Skip to content

Conversation

@dean0x7d
Copy link
Member

@dean0x7d dean0x7d commented Oct 16, 2016

This prevents unwanted conversions of py::object to bool or int:

py::object my_object;

std::cout << my_object << std::endl; // compiles and prints 0 or 1
int n = my_object; // compiles because of secondary implicit conversion 
                   // of `bool` to `int` <-- nonsense

With explicit operator bool() the above cases become compiler errors. The output streams will only compile when pybind11/stl.h is included, as intended. if statements and logical expressions are considered explicit conversions, so they work as intended.

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.
@wjakob
Copy link
Member

wjakob commented Oct 17, 2016

Great, thank you!

@wjakob wjakob merged commit 3599585 into pybind:master Oct 17, 2016
@dean0x7d dean0x7d deleted the explicit-bool branch October 18, 2016 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants