I need to use unordered_map with function pointer passing in, is there any workaround which can make the following code work?
struct eq_fun
{
bool operator()(void* s1, const void* s2) const
{
return ( _cmp_fn((void*)s1,(void*)s2) == 0 );
}
int (*_cmp_fn)(void*, void*);
eq_fun(int (*fn)(void*, void*)):_cmp_fn(fn){}
};
struct hash_fun
{
size_t operator()(const void *p) const
{
return _hash_fn(p);
}
int (*_hash_fn)(const void*);
hash_fun(int (*fn)(const void*)):_hash_fn(fn){}
};
unordered_map<void*,void*> *create(int (*h)(const void*),int (*cmp)(void*,void*))
{
return new unordered_map<void*,void*,hash_fun(h),eq_fun(cmp)>;
}
Sure
unordered_map<void*,void*,hash_fun, eq_fun> *create(int (*h)(const void*),int (*cmp)(void*,void*))
{
return new unordered_map<void*,void*,hash_fun,eq_fun>(0, hash_fun(h), eq_fun(cmp));
}
Seems unordered_map does not have a constructor that takes only the hash function and equaility function, so I've added the minimum number of buckets parameter with a value of zero. In any case the important point is that you construct your function objects separately and pass those objects to the unordered_map constructor.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments