【发布时间】:2016-03-03 02:39:19
【问题描述】:
我在采用可变参数模板的函数中使用 std::placeholders 时遇到了困难。我开始认为我不需要使用可变参数模板,但我已经尝试了很长时间,我在杂草中思考,需要一个焕然一新的人。
以下模板函数采用常规模板参数,然后是可变参数。它导致编译器错误:
registerEvent<UpdaterComponent, std::_Ph<1>>(EVT_INIT, &UpdaterComponent::initialise, std::placeholders::_1);
编译器错误:
错误 1 错误 C2664: 'Status Component::registerEvent>(const int &,Status UpdaterComponent::* (__cdecl )(const EventArgs &),std::_Ph)' : 无法转换参数2 从 'Status (__thiscall UpdaterComponent:: )(const EventArgs &)' 到 'Status UpdaterComponent::* (__cdecl *)(const EventArgs &)'
这里到底出了什么问题,我该如何解决这个编译器错误?
// In class Component
template<typename T, typename... Params>
Status registerEvent(int evtId, Status T::*func(const EventArgs& evtArgs), Params...)
{
... irrelevant code removed for brevity
auto res = std::bind(func, this, Params...);
... irrelevant code removed for brevity
}
// Usage
class UpdaterComponent : public Component
{
public:
UpdaterComponent()
{
registerEvent<UpdaterComponent, std::_Ph<1>>(EVT_INIT, &UpdaterComponent::initialise, std::placeholders::_1);
}
};
【问题讨论】:
-
你能显示
UpdaterComponent::initialise吗? -
另外,
Status T::*func(const EventArgs& evtArgs)应该是Status (T::*func)(const EventArgs& evtArgs)
标签: c++ templates c++11 variadic-templates