Some context...
- I have a map containing vectors of objects as values.
- Each vector contains objects of specialized type.
- The keys of the maps are the values of an enum representing each types, EnumObjType
- I am receiving events containing an EnumObjType and an ObjID.
I am trying to build a template function to process any type of object, and my challenge is that these objects IDs are accessed with methods of the specialized class. To work around that, I am trying to add a parameter to my template function that would be the function to be called to get the SpecializedObj's ID.
The template function call looks like this :
bool processEvent<SpecializedObj>(iEvent, iObjMap, &SpecializedObj::getSpecializedId);
The definition :
template<typename ObjType>
bool processEvent(MyEvent* iEvent, ObjMap* iPubObjMap, 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
}
And below the rather cryptic error to my eyes:
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> ]
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.
EDIT: I'm using the Visual Studio 2012 v110 toolset
Aucun commentaire:
Enregistrer un commentaire