Tuesday 16 July 2019

c++ - unable to pass Template function as a callback parameter

Please refer the code below:


typedef void (*TimerCallback)(int RequestID_in, void* AdditionalParameter_in);
class MyTimer
{
public:
MyTimer(){}
bool schedule( int Interval_in, TimerCallback TimerCallback_in, void* AdditionalParameter_in)
{
//some logic
return true;
}
};
namespace
{
template
void myTimerFunc(int RequestID_in, void* AdditionalParameter_in)
{
MyLogic* pLogic = static_cast*>(AdditionalParameter_in);
if(pLogic)
{
//do something
}
}
}
template
class MyLogic
{
public:
MyLogic(){}
void testMe()
{
MyTimer aTimer;
aTimer.schedule(10, myTimerFunc, this);
}
};
int main()
{
MyLogic myLogic;
myLogic.testMe();
}

I am using VC6 compiler and the compiler throws following error:



error C2664: 'schedule' : cannot
convert parameter 2 from 'void
(int,void *)' to 'void (__cdecl
*)(int,void *)'
None of the functions with this name in scope match the target
type
E:\test\BTest\BTest.cpp(46) : while compiling class-template member
function 'void __thiscall
MyLogic::testMe(void)'



I tested this code in Visual Studio 2008 and it works fine without any issues.


I know VC6 is an outdated compiler but my project source code(legacy) is still compiled with VC6.


Hence, any work around possible to make this code compile?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...