I'm working with google sparse hashmap library. And I have the following class template:
template <class Key, class T,
class HashFcn = std::tr1::hash<Key>,
class EqualKey = std::equal_to<Key>,
class Alloc = libc_allocator_with_realloc<std::pair<const Key, T> > >
class dense_hash_map {
.....
typedef dense_hashtable<std::pair<const Key, T>, Key, HashFcn, SelectKey,
SetKey, EqualKey, Alloc> ht;
.....
};
Now I have defined my own class as:
class my_hashmap_key_class {
private:
unsigned char *pData;
int data_length;
public:
// Constructors,Destructor,Getters & Setters
//equal comparison operator for this class
bool operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2) const;
//hashing operator for this class
size_t operator()(const hashmap_key_class &rObj) const;
};
Now I want to pass my_hashmap_key_class
as a Key
, my_hashmap_key_class::operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2)
as EqualKey
and my_hashmap_key_class::operator()(const hashmap_key_class &rObj)
as HashFcn
to dense_hash_map
class as parameters while using it in main function as:
main.cpp:
dense_hash_map<hashmap_key_class, int, ???????,???????> hmap;
What is the proper way of passing the class member functions as template parameters??
I tried passing like:
dense_hash_map<hashmap_key_class, int, hashmap_key_class::operator(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2),hashmap_key_class::operator()(const hashmap_key_class &rObj)> hmap;
But I get compilation error as operator is not detected. Please help me realize where I'm doing wrong.
As discussed in the comments, you should write the equality as an operator==
. Also, either make these operators static or remove one of their parameters (the "this" pointer will be the left hand operand for the equality test), otherwise they won't work as expected.
//equal comparison operator for this class
bool operator==(const hashmap_key_class &rObj2) const;
//hashing operator for this class
size_t operator()() const;
Then, your class is ready for client code like this:
my_hashmap_key_class a = ...;
my_hashmap_key_class b = ...;
a == b; // calls a.operator==(b), i.e. compares a and b for equality
a(); // calls a.operator()(), i.e. computes the hash of a
Then, you should be fine by using the default template parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.