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"
Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.
Can there be more than one argument to templates? Yes, like normal parameters, we can pass more than one data type as arguments to templates.
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
Use a std::function
instead:
AMap<G,int, std::function<int(G&)>> m;
Edit:
You could change your AMap
class as follows:
template<typename K, typename T, typename F>
class AMap {
int getIndex(K& k) { return K_uniqueHash(k); }
// ...
};
Suppossed that you have a class Foo
with a member function bar
:
struct Foo {
int bar(G&);
};
You could pass member functions as well as lambdas etc as:
AMap<G,int, std::function<int(G&)>> m;
auto f = [](G &i)->int { return 42; };
m[0] = f; // if your class works like a map
Foo foo;
m[2] = std::bind(&Foo::bar, &foo, std::placeholders::_1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With