模板函数在模板对象上执行成员回调而不提供它的实例
Template function to execute member callback on templated object without providing it's instance
一些上下文...
- 我有一个包含对象向量作为值的地图。
- 每个向量都包含特定类型的对象。
- 映射的键是代表每种类型的枚举值,
EnumObjType
- 我正在接收包含
EnumObjType
和 ObjID
的事件。
我正在尝试构建一个模板函数来处理任何类型的对象,而我面临的挑战是这些对象 ID 是通过专用 class 的方法访问的。为了解决这个问题,我正在尝试向我的模板函数添加一个参数,该函数将被调用以获取 SpecializedObj
的 ID。
模板函数调用如下所示:
bool processEvent<SpecializedObj>(iEvent, iObjMap, &SpecializedObj::getSpecializedId);
定义:
template<typename ObjType>
bool processEvent(MyEvent* iEvent, ObjMap* iObjMap, std::function<unsigned int(void)> iObjIdCall)
{
// Reach the corresponding ObjType vector
ObjMap::iterator it = iObjMap->find(iEvent->getObjType());
std::vector<BaseObject*> wObjVector = it->second;
// Iterate over the vector's range
for(BaseObject* wBaseObjPtr : wObjVector)
{
// Cast into SpecializedObj
ObjType* wSpecializedObjPtr = dynamic_cast<ObjType*>(wBaseObjPtr);
// Test dynamic_cast result
if(!wSpecializedObjPtr) {return false;}
std::function<unsigned int(void)> callback = std::bind(iObjIdCall,*wSpecializedObjPtr);
// Check if the SpecializedObjPtr's ID matches the event's DeviceId
if(callback() != iEvent->getDeviceId()) {return false;}
// work on SpecializedObj
// ...
return true
}
下面是我眼中相当神秘的错误:
11>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : error C2664: 'std::_Func_class<_Ret>::_Set' : cannot convert parameter 1 from '_Myimpl *' to 'std::_Func_base<_Rx> *'
11> with
11> [
11> _Ret=const unsigned int
11> ]
11> and
11> [
11> _Rx=const unsigned int
11> ]
11> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Do_alloc<_Myimpl,_Fret(__thiscall SpecializedObj::* const &)(void) const,_Alloc>(_Fty,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>,
11> _Fty=const unsigned int (__thiscall SpecializedObj::* const &)(void) const
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Do_alloc<_Myimpl,_Fret(__thiscall SpecializedObj::* const &)(void) const,_Alloc>(_Fty,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>,
11> _Fty=const unsigned int (__thiscall SpecializedObj::* const &)(void) const
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset_alloc<_Fret,SpecializedObj,std::allocator<_Ty>>(_Fret (__thiscall SpecializedObj::* const )(void) const,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Ty=std::_Func_class<const unsigned int>,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset_alloc<_Fret,SpecializedObj,std::allocator<_Ty>>(_Fret (__thiscall SpecializedObj::* const )(void) const,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Ty=std::_Func_class<const unsigned int>,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset<const unsigned int,SpecializedObj>(_Fret (__thiscall SpecializedObj::* const )(void) const)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset<const unsigned int,SpecializedObj>(_Fret (__thiscall SpecializedObj::* const )(void) const)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int
11> ]
11> C:\MyProject\ObjUpdater.cpp(29) : see reference to function template instantiation 'std::function<_Fty>::function<const unsigned int(__thiscall SpecializedObj::* )(void) const>(_Fx &&)' being compiled
11> with
11> [
11> _Fty=const unsigned int (void),
11> _Fx=const unsigned int (__thiscall SpecializedObj::* )(void) const
11> ]
11> C:\MyProject\ObjUpdater.cpp(29) : see reference to function template instantiation 'std::function<_Fty>::function<const unsigned int(__thiscall SpecializedObj::* )(void) const>(_Fx &&)' being compiled
11> with
11> [
11> _Fty=const unsigned int (void),
11> _Fx=const unsigned int (__thiscall SpecializedObj::* )(void) const
11> ]
当我以前使用回调时,我总是知道它会与哪个对象实例相关联,所以我将它作为函数参数传递给 std::bind(&Foo::func, fooObj)
,但它在这里似乎对我不起作用.
编辑:
我正在使用 Visual Studio 2012 v110 工具集
When I had previously worked with callbacks, I always knew with what object instance it would be associated, so I'd pass it as function parameters with std::bind(&Foo::func, fooObj)
but it does not seem to work for me here.
如果 processEvent()
将 iObjIdCall
作为 std::function<unsigned int(void)>
接收,当您尝试绑定它并保存 std::bind()
结果为相同类型的对象时
std::function<unsigned int(void)> callback
= std::bind(iObjIdCall,*wSpecializedObjPtr);
你混淆了编译器。
尝试将 processEvent()
定义为接收指向方法的指针而不是 std::function
;像
template <typename ObjType>
bool processEvent (MyEvent* iEvent, ObjMap* iObjMap, ObjType::*iObjIdCall)
如果我没有记错的话。
一些上下文...
- 我有一个包含对象向量作为值的地图。
- 每个向量都包含特定类型的对象。
- 映射的键是代表每种类型的枚举值,
EnumObjType
- 我正在接收包含
EnumObjType
和ObjID
的事件。
我正在尝试构建一个模板函数来处理任何类型的对象,而我面临的挑战是这些对象 ID 是通过专用 class 的方法访问的。为了解决这个问题,我正在尝试向我的模板函数添加一个参数,该函数将被调用以获取 SpecializedObj
的 ID。
模板函数调用如下所示:
bool processEvent<SpecializedObj>(iEvent, iObjMap, &SpecializedObj::getSpecializedId);
定义:
template<typename ObjType>
bool processEvent(MyEvent* iEvent, ObjMap* iObjMap, std::function<unsigned int(void)> iObjIdCall)
{
// Reach the corresponding ObjType vector
ObjMap::iterator it = iObjMap->find(iEvent->getObjType());
std::vector<BaseObject*> wObjVector = it->second;
// Iterate over the vector's range
for(BaseObject* wBaseObjPtr : wObjVector)
{
// Cast into SpecializedObj
ObjType* wSpecializedObjPtr = dynamic_cast<ObjType*>(wBaseObjPtr);
// Test dynamic_cast result
if(!wSpecializedObjPtr) {return false;}
std::function<unsigned int(void)> callback = std::bind(iObjIdCall,*wSpecializedObjPtr);
// Check if the SpecializedObjPtr's ID matches the event's DeviceId
if(callback() != iEvent->getDeviceId()) {return false;}
// work on SpecializedObj
// ...
return true
}
下面是我眼中相当神秘的错误:
11>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : error C2664: 'std::_Func_class<_Ret>::_Set' : cannot convert parameter 1 from '_Myimpl *' to 'std::_Func_base<_Rx> *'
11> with
11> [
11> _Ret=const unsigned int
11> ]
11> and
11> [
11> _Rx=const unsigned int
11> ]
11> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Do_alloc<_Myimpl,_Fret(__thiscall SpecializedObj::* const &)(void) const,_Alloc>(_Fty,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>,
11> _Fty=const unsigned int (__thiscall SpecializedObj::* const &)(void) const
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Do_alloc<_Myimpl,_Fret(__thiscall SpecializedObj::* const &)(void) const,_Alloc>(_Fty,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>,
11> _Fty=const unsigned int (__thiscall SpecializedObj::* const &)(void) const
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset_alloc<_Fret,SpecializedObj,std::allocator<_Ty>>(_Fret (__thiscall SpecializedObj::* const )(void) const,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Ty=std::_Func_class<const unsigned int>,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(514) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset_alloc<_Fret,SpecializedObj,std::allocator<_Ty>>(_Fret (__thiscall SpecializedObj::* const )(void) const,_Alloc)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int,
11> _Ty=std::_Func_class<const unsigned int>,
11> _Alloc=std::allocator<std::_Func_class<const unsigned int>>
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset<const unsigned int,SpecializedObj>(_Fret (__thiscall SpecializedObj::* const )(void) const)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int
11> ]
11> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(675) : see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset<const unsigned int,SpecializedObj>(_Fret (__thiscall SpecializedObj::* const )(void) const)' being compiled
11> with
11> [
11> _Ret=const unsigned int,
11> _Fret=const unsigned int
11> ]
11> C:\MyProject\ObjUpdater.cpp(29) : see reference to function template instantiation 'std::function<_Fty>::function<const unsigned int(__thiscall SpecializedObj::* )(void) const>(_Fx &&)' being compiled
11> with
11> [
11> _Fty=const unsigned int (void),
11> _Fx=const unsigned int (__thiscall SpecializedObj::* )(void) const
11> ]
11> C:\MyProject\ObjUpdater.cpp(29) : see reference to function template instantiation 'std::function<_Fty>::function<const unsigned int(__thiscall SpecializedObj::* )(void) const>(_Fx &&)' being compiled
11> with
11> [
11> _Fty=const unsigned int (void),
11> _Fx=const unsigned int (__thiscall SpecializedObj::* )(void) const
11> ]
当我以前使用回调时,我总是知道它会与哪个对象实例相关联,所以我将它作为函数参数传递给 std::bind(&Foo::func, fooObj)
,但它在这里似乎对我不起作用.
编辑: 我正在使用 Visual Studio 2012 v110 工具集
When I had previously worked with callbacks, I always knew with what object instance it would be associated, so I'd pass it as function parameters with
std::bind(&Foo::func, fooObj)
but it does not seem to work for me here.
如果 processEvent()
将 iObjIdCall
作为 std::function<unsigned int(void)>
接收,当您尝试绑定它并保存 std::bind()
结果为相同类型的对象时
std::function<unsigned int(void)> callback
= std::bind(iObjIdCall,*wSpecializedObjPtr);
你混淆了编译器。
尝试将 processEvent()
定义为接收指向方法的指针而不是 std::function
;像
template <typename ObjType>
bool processEvent (MyEvent* iEvent, ObjMap* iObjMap, ObjType::*iObjIdCall)
如果我没有记错的话。