【发布时间】:2019-02-24 18:20:29
【问题描述】:
我希望能够传递一个函数,该函数采用任意数量的参数以及匹配的参数作为函数的参数。这是我迄今为止尝试过的,我迷路了,因为我不太了解参数包的工作原理。
template <class T, typename... Args>
static void BindAction(const std::string& name, T* owner, const std::function<void(T*, Args...)>& func, Args... args)
{
for (InputAction& action : m_actions)
if (action.m_name == name)
action.BindFunction(std::bind(func, owner, args...));
}
我用这些来测试它:
void Test(float f, std::string s, int i);
BindAction<Simulation>("Test", this, &Simulation::Test, 5.f, "a string", 2);
错误只是没有函数模板“BindAction()”的实例与参数列表匹配。
请不要通过告诉我调用std::bind() 然后将结果函数作为参数传递来解决问题。我真的希望电话是这样的。如果这是不可能的,那就太糟糕了,但只有这样我才会检查其他解决方案。
【问题讨论】:
-
成员函数不是 std::function。函数模板需要(几乎)完全匹配,不会为您插入隐式用户定义的转换。
-
“成员函数不是 std::function”是什么意思?如果我去掉添加参数的概念,我完全可以将成员函数作为 std::function 传递。
-
我不明白 `return action.BindFunction(std::bind(func, owner, args...));` 那行应该做什么?你只是想调用函数吗?
-
另外,你考虑的是标准函数还是成员函数?要调用成员函数,需要
this某处,同意吗? -
我正在将函数添加到地图中。这部分无关紧要,因为接下来的工作已经开始了。是的,我将
this作为参数传递给函数,这是“T*”
标签: c++ variadic-templates std-function template-argument-deduction stdbind