Opened 10 years ago
Closed 10 years ago
#7489 closed Bugs (wontfix)
No hash function for classes containing conversion operation to ordinary type
| Reported by: | Owned by: | Daniel James | |
|---|---|---|---|
| Milestone: | To Be Determined | Component: | hash |
| Version: | Boost 1.51.0 | Severity: | Problem |
| Keywords: | Cc: |
Description
Hi
It seems boost::hash no longer supports calculating hashes for classes that are convertible to int. It used to work with previous versions of boost::hash
Please see the following example (based on example from bug#7437), which compiles fine with boost::hash up to version 1.50, but fails to compile with boost 1.51
#include <boost/unordered_map.hpp>
class MyClass
{
public:
MyClass(int i=0): v(i) {}
operator int() const {return v;}
private:
int v;
};
typedef boost::unordered_map<MyClass,std::string> MyMap;
int main(int argc, char* argv[])
{
MyMap m;
MyMap::value_type v(MyClass(1),"apples");
m.insert(v);
return 0;
}
The error is:
/opt//boost_1_51_0/include/boost/functional/hash/extensions.hpp:257: error: no matching function for call to 'hash_value(const MyClass&)'
Change History (2)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.

This is deliberate, implicit conversions can cause subtle errors. For a case like this it's pretty easy to add support with something like:
friend std::size_t hash_value(MyClass const& v) { return (int) v; }