Error C2975 in a template function implementation

2019-08-16 14:26发布

问题:

I've asked a similar question already but did not provide enough details. So I'm trying to re-phrase it with a better code sample. So here it is.

First off I'm using Visual Studio 2008 C++ compiler and I'm trying to implement a template to call a member method of a class.

The template is declared as such:

typedef void (*ON_CALL)(HWND);

template <typename CLASS, ON_CALL F>
class CMyTemplate
{
public:
    static void onProcess(CLASS* pDlg)
    {
        pDlg->SetTimer(100, 1, _onTimer);
    }

private:
    static void WINAPI _onTimer(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
    {
        KillTimer(hwnd, idEvent);

        CLASS* pDlg = (CLASS*)CDialog::FromHandle(hwnd);
        if(pDlg)
        {
            pDlg->F(hwnd);
        }
    }
};

And it's actually called like so:

void CMyDialog::SomeMethod()
{
    CMyTemplate<CMyDialog, OnCall>::onProcess(this);    //error C2975
}

void CMyDialog::OnCall(HWND hWnd)
{
    //Do work ...
}

But that line that I marked above gives me this error:

error C2975: 'F' : invalid template argument for 'CMyTemplate', expected compile-time constant expression

回答1:

function OnCall is class member which implicit a this pointer, so it will not match the declaration