I thought I could have a pointer to a fully-specialized template function, but the following code isn't compiling (MSVC2012)
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
unsigned long hashing_func(string key)
{
unsigned long hash = 0;
for(int i=0; i<key.size(); i++)
{
hash += (71*hash + key[i]) % 5;
}
return hash;
}
bool key_equal_fn2(string t1, string t2)
{
return t1 == t2;
}
template<class T> bool key_equal_fn(T t1, T t2)
{
return t1 == t2;
}
template <> bool key_equal_fn<string>(string t1, string t2)
{
return !(t1.compare(t2));
}
int main ()
{
unordered_map<string, string>::size_type n = 5;
unordered_map<string, string> mymap(n, (const std::hash<string> &)hashing_func, (const std::equal_to<string> &)(key_equal_fn<string>)) ;
mymap["paul"] = "jenna";
mymap["frank"] = "ashley";
return 0;
}
The constructor line is returning the following error:
See Question&Answers more detail:oserror C2440: 'type cast' : cannot convert from 'bool (__cdecl *)(T,T)' to 'const std::equal_to<_Ty> &'