I successfully passed a function as parameter.
// this is in a scope of a normal function
class DummyClass{
public: static int dummyFunction(G& goo){
return goo.doSomething (); //non-static function
//Edit 3: it calculates hash value
}
};
AMap<G,int,DummyClass::dummyFunction>map;
//... do some other thing
Those Dummy reduce readability of the code.
Can I call it in a more concise way?
AMap<G,int,
[](G&goo)->int{ return goo.doSomething (); }
>map;
I tried, but the compiler said
expected compile-time constant expression
It looks like compiler thought that the lambda function is not compile-time constant, but I am sure its behavior is.
I have read How to use a lambda expression as a template parameter? , but no solution can offer 1-statement way.
I would be ideal if I can call it like
AMap<G,int, G::doSomething >map; //note that G::doSomething is non-static
Edit
This is how I declared AMap
template<class K,class T,int (* K_uniqueHash)(K&) >AMap {//<--- can be changed
private: int getIndex(K& k){
return K_uniqueHash(k); //<--- can be changed
}
//.. other function
}
Your answer can also change codes of the above class.
Edit 2: Any modification toward AMap doesn't count as additional lines, because it is a library.
Edit 3: Sorry, my template may be misleading.
A map only use 1 function for hashing.
template<class K,class T,int (* K_uniqueHash)(K&) >AMap
^key ^value ^ hashing function
Therefore, I don't expect to assign 1 function per 1 key.
In other words, loosely speaking ....
AMap<K,T,k_hasher> aMap;
K k1,k2; T t1,t2;
aMap[ k1 ] = t1; aMap[ k2 ] =t2;
// Then, these statements below will be called internally.
k1.k_hasher();
k2.k_hasher(); //every k call same function "k_hasher"