How to extend std::tr1::hash for custom types?
Asked Answered
R

4

22

How do I allow the STL implementation to pick up my custom types? On MSVC, there is a class std::tr1::hash, which I can partially specialize by using

namespace std 
{
    namespace tr1 
    { 
        template <> 
        struct hash<MyType> 
        { ... };
    } 
}

but is this the recommended way? Moreover, does this work with GCC's implementation as well? For boost::hash, it's enough to provide a free function size_t hash_value (const MyType&), is there something similar for the TR1 implementation?

Richburg answered 15/3, 2009 at 15:14 Comment(2)
Is there a way to extend std::hash for user-defined types with private copy constructors? Also, is there a way to extend it with an operator() that takes a const ref instead of val?Bushtit
What's the problem with the template specialization? You don't take a copy of your object (you pass it by references), so no problem -- and the operator() takes a const ref or value, whatever you want. Look at Phil Nash's answer, which takes the object as const ref.Richburg
U
4

Yes, this will also work for GCC. I'm using it in a bigger project and it works without problems. You could also provide your own custom hashing class for the TR1 containers, but it is specified that std::tr1::hash<> is the default hashing class. Specializing it for custom types seems like the natural way to extend the standard hashing functionality.

Unreserve answered 15/3, 2009 at 15:24 Comment(0)
T
21

I was trying to work out the exact syntax for doing this with the unordered associative containers (also using GCC, as the OP was asking) and hit this question.

Unfortunately it didn't go down to the level of detail I wanted. By looking through the gcc headers at how they have implemented the standard hash functions I got it working. In view of the dearth of examples (at least at time of writing) on the web I thought this would be as good a place as any to post my own example (which I can confirm works with GCC):


namespace std { namespace tr1
{
   template <>
   struct hash<MyType> : public unary_function<MyType, size_t>
   {
       size_t operator()(const MyType& v) const
       {
           return /* my hash algorithm */;
       }
   };
}}

(notice there are two namespaces here - that's just my convention for collapsing nested namespaces)

Twickenham answered 5/4, 2009 at 14:27 Comment(0)
U
4

Yes, this will also work for GCC. I'm using it in a bigger project and it works without problems. You could also provide your own custom hashing class for the TR1 containers, but it is specified that std::tr1::hash<> is the default hashing class. Specializing it for custom types seems like the natural way to extend the standard hashing functionality.

Unreserve answered 15/3, 2009 at 15:24 Comment(0)
D
3

As you are not adding to std library namespace, but only providing the specialisations, then it is perfectly OK.

If you want to provide more generic hashing approach (for example hash for the tuples in general) then have a look at Boost Fusion. Here is a simple example, which will work for most of the cases (probably with the exception for tuple of tuples)

Demolition answered 15/3, 2009 at 15:44 Comment(0)
P
0

The following code snippet shows how to specialize std::tr1::unordered_map to mapping boost::const_string<char> to void* analogously with how std::string is hashed.

#include <boost/const_string/const_string.hpp>    
typedef class boost::const_string<char> csc;

namespace std
{
namespace tr1
{
template <>
struct hash<csc> {
public:
    size_t operator()(const csc & x) const {
        return std::_Hash_impl::hash(x.data(), x.size());
    }
};
}
}

typedef std::tr1::unordered_map<csc, void*> Map;
typedef Map::value_type Dual; ///< Element Type.
Propene answered 5/5, 2011 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.