7
7
BSD-style license that can be found in the LICENSE file.
8
8
*/
9
9
10
- #include " pybind11_tests.h"
11
10
#include " constructor_stats.h"
12
- #include < pybind11/operators.h >
11
+ #include " pybind11_tests.h "
13
12
#include < functional>
13
+ #include < pybind11/operators.h>
14
+ #include < pybind11/stl.h>
14
15
15
16
class Vector2 {
16
17
public:
@@ -71,6 +72,12 @@ int operator+(const C2 &, const C2 &) { return 22; }
71
72
int operator +(const C2 &, const C1 &) { return 21 ; }
72
73
int operator +(const C1 &, const C2 &) { return 12 ; }
73
74
75
+ struct HashMe {
76
+ std::string member;
77
+ };
78
+
79
+ bool operator ==(const HashMe &lhs, const HashMe &rhs) { return lhs.member == rhs.member ; }
80
+
74
81
// Note: Specializing explicit within `namespace std { ... }` is done due to a
75
82
// bug in GCC<7. If you are supporting compilers later than this, consider
76
83
// specializing `using template<> struct std::hash<...>` in the global
@@ -82,6 +89,14 @@ namespace std {
82
89
// Not a good hash function, but easy to test
83
90
size_t operator ()(const Vector2 &) { return 4 ; }
84
91
};
92
+
93
+ // HashMe has a hash function in C++ but no `__hash__` for Python.
94
+ template <>
95
+ struct hash <HashMe> {
96
+ std::size_t operator ()(const HashMe &selector) const {
97
+ return std::hash<std::string>()(selector.member );
98
+ }
99
+ };
85
100
} // namespace std
86
101
87
102
// Not a good abs function, but easy to test.
@@ -228,8 +243,12 @@ TEST_SUBMODULE(operators, m) {
228
243
.def (" __hash__" , &Hashable::hash)
229
244
.def (py::init<int >())
230
245
.def (py::self == py::self);
231
- }
232
246
247
+ // define __eq__ but not __hash__
248
+ py::class_<HashMe>(m, " HashMe" ).def (py::self == py::self);
249
+
250
+ m.def (" get_unhashable_HashMe_set" , []() { return std::unordered_set<HashMe>{{" one" }}; });
251
+ }
233
252
#if !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
234
253
#pragma GCC diagnostic pop
235
254
#endif
0 commit comments