从成员函数模板化参数调用成员函数

Sca*_*ark 0 c++ templates member-function-pointers function-pointers pointer-to-member

鉴于以下代码,我无法编译.

    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;
}
Run Code Online (Sandbox Code Playgroud)

我收到了folling错误消息

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

我以为我在使用.*但我显然有些不对劲.

我如何调用成员函数?

我试过了

return obj.*(template KM)();
return obj.*template (KM)();
return obj.template *(KM)();
Run Code Online (Sandbox Code Playgroud)

这些都没有奏效.

Gar*_*ell 5

正确的语法是

return (obj.*KM)();
Run Code Online (Sandbox Code Playgroud)