Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a custom function as template parameter within 1 statement

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"
like image 331
javaLover Avatar asked Jun 07 '16 08:06

javaLover


People also ask

How do you call a function in a template?

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 template?

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.

Can function parameter template?

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.

How do you use template arguments in C++?

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.)


1 Answers

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);
like image 184
101010 Avatar answered Oct 21 '22 04:10

101010