I want to know, if it is possible to build a name to function table in c++, something like map<string, function handle>. But
-
those functions have different signatures. I can assume they have same return type as
void. -
I was thinking define something like,
struct ftable { std::string name; void (void* pfun)(); // this is only for one function signature };
But how to make it work for different types of function?
I asked a related question here. In that question, I try to store functions into some container, but I realize that I can not store function with placeholder (see following code). Is this possible with c++? Thanks!
template <typename F, typename ... Args>
std::function<void()> MapFun (F const & f, Args const & ... args)
{ return [=]{ f(args...); }; }
void userFun1 (int i1, int i2)
{ std::cout << "uf1, " << i1 << ", " << i2 << std::endl; }
int main ()
{
auto l1 = MapFun(userFun1, 1, 2);
std::unordered_map<std::string, std::function<void()>> func_map;
func_map["userFun1"] = std::bind(l1); // this is okay;
//func_map["userFun1"] = std::bind(userFun1, std::placeholders::_1, std::placeholders::_2); // this is wrong;
//auto lll1 = MapFun(userFun1, std::placeholders::_1,std::placeholders::_2); // also wrong.
}
Aucun commentaire:
Enregistrer un commentaire