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

发布于 2024-08-21 21:23:56 字数 874 浏览 1 评论 0原文

鉴于以下代码我无法编译。

    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;
}

我收到以下错误消息,

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

我以为我正在使用 .* 但我显然出了问题。

如何调用成员函数?

我试过了

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

,没有一个有效。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

节枝 2024-08-28 21:23:56

正确的语法是

return (obj.*KM)();

The correct syntax is

return (obj.*KM)();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文