Skip to content

Commit a06949a

Browse files
committed
Try using std::hash<std::type_index>, std::equal_to<std::type_index> everywhere.
From PR #4316 we know that types in the unnamed namespace in different translation units do not compare equal, as desired. But do types in named namespaces compare equal, as desired?
1 parent ee2b522 commit a06949a

File tree

1 file changed

+1
-26
lines changed

1 file changed

+1
-26
lines changed

include/pybind11/detail/internals.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,10 @@ inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) {
114114
// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name,
115115
// which works. If not under a known-good stl, provide our own name-based hash and equality
116116
// functions that use the type name.
117-
#if defined(__GLIBCXX__)
118117
inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; }
119-
using type_hash = std::hash<std::type_index>;
120-
using type_equal_to = std::equal_to<std::type_index>;
121-
#else
122-
inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) {
123-
return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
124-
}
125-
126-
struct type_hash {
127-
size_t operator()(const std::type_index &t) const {
128-
size_t hash = 5381;
129-
const char *ptr = t.name();
130-
while (auto c = static_cast<unsigned char>(*ptr++)) {
131-
hash = (hash * 33) ^ c;
132-
}
133-
return hash;
134-
}
135-
};
136-
137-
struct type_equal_to {
138-
bool operator()(const std::type_index &lhs, const std::type_index &rhs) const {
139-
return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0;
140-
}
141-
};
142-
#endif
143118

144119
template <typename value_type>
145-
using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
120+
using type_map = std::unordered_map<std::type_index, value_type>;
146121

147122
struct override_hash {
148123
inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {

0 commit comments

Comments
 (0)