Question

Given the following code which I can't get to compile.

    template < typename OT, typename KT, KT (OT::* KM)() const >
    class X
    {
    public:
        KT mfn( const OT & obj )
        {
            return obj.*(KM)();    // Error here.
        }
    };

    class O
    {
    public:
        int func() const
        {
            return 3;
        }
    };

    int main( int c, char *v[] )
    {
        int a = 100;

        X<  O, int, &O::func > x;

        O o;

        std::cout << x.mfn( o ) << std::endl;
}

I get the folling error message

error: must use '.*' or '->*' to call pointer-to-member function in '&O::func (...)'

I thought I was using .* but I've obviously got something wrong.

How do I call the member function ?

I've tried

return obj.*(template KM)();
return obj.*template (KM)();
return obj.template *(KM)();

None of which worked.

Was it helpful?

Solution

The correct syntax is

return (obj.*KM)();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top