C++为成员函数提供typedef模板的一种方法

C++ a way to have a typedef template to a member function?

本文关键字:方法 一种 成员 函数 typedef C++      更新时间:2023-10-16

标题几乎说明了一切!

有什么漂亮/简单的方法可以做到这一点吗?由于函数指针不能指向成员函数,并且成员函数需要知道成员所属的类,但在这种情况下,由于函数来自的类不同,因此没有此类信息。请只使用标准库!

template <class A>
typedef void (A::*Function)(Parameters p); 

您可以尝试将其包装在这样的结构中:

template <class T>
struct method_ptr
{
  typedef void (T::*Function)(Parameters p); 
};
class A
{
public:
  void foobar(Parameters);
};

// ...
method_ptr<A>::Function pfoobar = &A::foobar;

未测试,但这应该与C++11编译器一起使用:

template <typename T>
using Function = void (T::*)(Parameters p);

现在有趣的问题是,解决这个问题的问题是什么。

相关文章: