【发布时间】:2019-11-22 21:45:26
【问题描述】:
有没有一种简单的方法可以将键映射到具有可变数量参数(以及可能的可变返回类型?)的函数。这就是我的意思。
//three functions, each variable number of args
void one_arg(int a) { std::cout << "a"; }
void two_args(int a, double b) { std::cout << "a\t" << "b\n"; }
void three_args(int a, double b, const char* c) { std::cout << "a\t" << "b\t" << "c\n"; }
std::function<void(int)> f_onearg = one_arg;
std::function<void(int, double)> f_twoargs = two_args;
std::function<void(int, double, const char*)> f_threeargs = three_args;
//now what??
template<typename... Args>
std::map<std::string, std::function<void(Args...)>> map_of_fun{ {"one", one_arg }, {"two", two_args}, {"three",three_args} };
//this is how i would like to call it
map_of_fun["one"](4);
map_of_fun["two"](4, 9.34);
我做错了什么?不行,错误在模板的某个地方,但我不知道在哪里。
【问题讨论】:
标签: stdmap std-function