-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
py::enum_'s __int__
and __hash__
do not behave correctly when the underlying type of enum class is char
#1331
Comments
BTW, when defining the enum class: // pybind11 wrapper
py::enum_<Player>(mod, "Player", "Gomoku player types")
.value("white", Player::White)
.value("none", Player::None)
.value("black", Player::Black)
//.def(py::init<int>()) this line run into another error
.def("__int__", [](Player p) { return static_cast<int>(p); })
.def("__float__", [](Player p) { return static_cast<double>(p); })
.def("__hash__", [](Player p) { return static_cast<int>(p); })
.def("__neg__", [](Player p) { return -p; })
.def_static("calc_score", [](Player p, Player w) { return getFinalScore(p, w); }); the
And I don't have any idea on how to solve this _ (:з」∠) _…… |
Oh, I have solved the bug of def(py::init([](int p) { return Player(p); })) Maybe the form of creating an enum |
__int__
and __hash__
do not behave correctly when the underlying type of enum class is char
This smells like a bug: the basic (Your If you want to take a stab at a PR to fix it, I think changing |
Issue description
I am exposing an enum class to python, and this is my code:
As I was testing the module in python, I ran into such a strange issue:
In fact, similar issues have occurred when I pass the
py::arithmetic()
extra parameter to py::enum_.Then I checked the inner member functions and discovered these:
So, why does a
static_cast<int>
resulted in a str in python?Intuitively, I change the underlying type of
enum class Player
fromchar
toint
, and:Thus, it is the underlying type
char
ofenum class player
that causes all these bugs.The text was updated successfully, but these errors were encountered: