Failed to pass Template function as callback parameter

Please read the following code:

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 <class T>
    void myTimerFunc(int RequestID_in, void* AdditionalParameter_in)
    {
        MyLogic<T>* pLogic = static_cast<MyLogic<T>*>(AdditionalParameter_in);
        if(pLogic)
        {
            //do something
        }
    }
}

template <class T>
class MyLogic
{
public:
    MyLogic(){}

    void testMe()
    {
        MyTimer aTimer;
        aTimer.schedule(10, myTimerFunc<T>, this);
    }
};

int main()
{
    MyLogic<int> myLogic;
    myLogic.testMe();
}

I use the VC6 compiler and the compiler throws the 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 the region match the target type E : \ test \ BTest \ BTest.cpp (46): when compiling a member of the template class, the function 'void __thiscall MyLogic :: TestMe (are canceled)

I tested this code in Visual Studio 2008 and it works great without any problems.

I know that VC6 is an outdated compiler, but the source code for the project (legacy) is still compiled with VC6.

Therefore, any work around a possible compilation of this code?

+3
2

Visual Studio, , , / . , , id(), :

template<class T>
T id(T t) 
{
    return t;
}

template <class T>
class MyLogic
{
public:
    MyLogic(){}

    void testMe()
    {
        MyTimer aTimer;
        aTimer.schedule(10, id(myTimerFunc<T>), this);
        //------------------^^(              )
    }
};

Visual Studio 6. , myTimerFunc , .

+1

, __stdcall:

typedef void (__stdcall *TimerCallback)(int RequestID_in, void* AdditionalParameter_in);

, , , . , , __stdcall __cdecl .

0

Source: https://habr.com/ru/post/1758002/


All Articles