7

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"
5
  • 1
    Did you already try std::bind()? Commented Jun 7, 2016 at 8:18
  • 1
    A canonical way is to pass a type of a function object as a type-template argument, store it in a class, and initialize it in a class constructor Commented Jun 7, 2016 at 8:29
  • I guess that you mean : pass function as an argument - with in () - of AMap's constructor", right? It seems to require another line of code. Commented Jun 7, 2016 at 8:32
  • 2
    @javaLover, unless you are code golfing - that seems like an arbitrary requirement!?! Commented Jun 7, 2016 at 8:33
  • I really want to increase readability, because it will be in my core c++ library. Commented Jun 7, 2016 at 8:38

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Thank, but how can I tell that map to use a certain function like "G::doSomething"?
@javaLover member functions are not ordinary functions per se. They must be bind with an object of their respective class. For that you would need std::bind.
@javaLover for your example code, the typical pattern is to use a non-member non-friend function as the hashing function (see for example std::hash)...
@101010 : As far as I know, each call of std::bind require a pointer of object. I have looked into it, but don't know how to apply to this AMap. (with 1 line of code requirement) or you mean it is impossible? If so, please write it in your answer.
@101010 : (reply edited answer) Sorry, my template signature is misleading, the map is mapping form G -> int, not G -> function. (I have edited my question.) The function signature is assumed to be same across every member of a single map, so I shouldn't assign function as value of map like that. (only 1 function should be used for 1 map) With my low capability, I can't adapt your answer to the case of G->int as well. May you provide another example about G->int, please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.