c++ - How to use only 1 template value for a function in a class which has 2 templates values -


i'm trying implement hashmap in c++. class

template <typename k, typename v> class hashmap { public:     unsigned int map_size = 100;     v* values_arr = new v[map_size];     int hash(std::string); //i want 1 hash function std::string     int hash(k); //then used integral types  }; 

now function expect called k (key) string

template <typename k, typename v> int hashmap<std::string, v>::hash(std::string value) { //this line error     std::cout << "you hashing string";      return 0; } 

and 1 want used when key in integral

template <typename k, typename v> int hashmap<k, v>::hash(k value) {     std::cout << "you hashing an integral";      return 0; } 

i'm trying encode logic declaring function hashmap<std::string, v> think should work i'm not positive. error (i commented line it's on)

error c3860 template argument list following class template name must list parameters in order used in template parameter list hashmap

you instead take inspiration std::unordered_map, , take hash (and key equality) template parameter.

template <typename k> struct myhash; // don't know how hash arbitrary types  template <> struct myhash<std::string> {     int operator()(std::string s) {         std::cout << "you hashing string";         return 0;     } }  template <> struct myhash<int> {     int operator()(int i) {         std::cout << "you hashing int";         return i;     } }  template <typename k> struct myequal { // know how compare arbitrary types equality     bool operator()(k lhs, k rhs) {         return lhs == rhs;     } }  template <typename k, typename v, typename hash = myhash<k>, typename equal = myequal<k>> class hashmap { private:     hash hasher;     equal comparer;     unsigned int map_size = 100;     v* values_arr = new v[map_size]; public:     int hash(k key) { return hasher(key); } // calls hash::operator()(k)     bool equal(k lhs, k rhs) { return comparer(lhs, rhs); } // calls equal::operator()(k, k)     // ... other members }; 

then, extension, can things like

struct caseinsensitivestringhash : private myhash<std::string> {     int operator()(std::string s) { return myhash::operator()(s.to_lower()); } }  struct caseinsensitivestringequal : private myequal<std::string> {     bool operator()(std::string lhs, std::string rhs) {          return myequal::operator()(lhs.to_lower(), rhs.to_lower());      } }  template <typename v> using caseinsensitivestringmap = hashmap<std::string, v, caseinsensitivestringhash, caseinsensitivestringequal>; 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -