在 gcc 上将成员函数指针传递到模板化成员函数时出现问题

Having problem passing member function pointers to templatized member function on gcc

本文关键字:函数 成员 问题 gcc 指针      更新时间:2023-10-16

>我在将成员函数指针传递到 gcc 上的模板化成员函数时遇到问题。 有谁知道如何修改下面的代码以使 gcc 接受我正在尝试做的事情?

class Foo
{
public:
    template <class C, class R>
    void Execute(R(typename C::*memFn)())
    {
    }
};

尝试编译代码时出现以下错误:

test.cpp:40: error: 'memFn' was not declared in this scope
test.cpp:40: error: expected primary-expression before '(' token
test.cpp:40: error: expected identifier before '*' token
test.cpp:40: error: expected '(' before '*' token
test.cpp:40: error: 'memFn' was not declared in this scope
test.cpp:40: error: variable or field 'Execute' declared void

我正在使用的 gcc 版本是 4.4.2。

非常感谢您的帮助!

你不需要typename .删除它,它应该可以工作。(我在 gcc 4.3.2 上对其进行了测试)。