Search code examples
c++visual-studiocom

C++ Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member”


I work with TaskScheduler COM, this is my code:

typedef HRESULT(*FuncOfBoll)(_Out_ VARIANT_BOOL* b);

static bool GetBool(FuncOfBoll func)
{
    VARIANT_BOOL b = VARIANT_FALSE;
    HRESULT hr = func(&b);
    if (FAILED(hr)) return FALSE;
    return b == VARIANT_TRUE;
}

void test(ITaskSettings* settings)
{
    bool b = GetBool(settings->get_StopIfGoingOnBatteries); // <= The error here
    // ...
}

and I get the following error:

Error C3867 'ITaskSettings::get_StopIfGoingOnBatteries': non-standard syntax; use '&' to create a pointer to member

What is my mistake and how to correct it?


Solution

  • The correct definition for a pointer to member function is:

    typedef HRESULT(ITaskSettings::*FuncOfBoll)(_Out_ VARIANT_BOOL* b);
    

    Then, you should pass the pointer to the object instance to function GetBool:

    static bool GetBool(ITaskSettings* setting, FuncOfBoll func)
    {
        VARIANT_BOOL b = VARIANT_FALSE;
        HRESULT hr = (setting->*func)(&b);
        ...
    }
    

    Or, with template:

    template<class C>
    static bool GetBool(C* p, HRESULT(C::*func)(_Out_ VARIANT_BOOL*))
    {
        VARIANT_BOOL b = VARIANT_FALSE;
        HRESULT hr = (p->*func)(&b);
        ...
    }
    

    Invocation:

    void test(ITaskSettings* settings)
    {
        currentSetttings = settings;
        bool b = GetBool(settings, &ITaskSettings::mb_function);
    }