使用类似模板的机制调用具有相同签名的不同方法

Calling different methods with the same signature using a template like mechanism

本文关键字:方法 调用 机制      更新时间:2023-10-16

我有这个代码:

struct C
{
   int d1;
   int d2;
};
struct A
{
      void  write(C data)
      {
      }
};
struct B
{
      void use(C data)
      {
      }
};

现在,我想定义一个使用 AB 的新类,并调用它们的 writeuse 方法。像这样的东西:

template <class T>
struct D
{
     T t;
     D(T myT) { t=myT; }
     void myFunct(C data)
     {
         // t.????(data);
     }
};

如您所见,如果这两个类具有相似的方法名称,那么D实现就很容易了,但是由于AB有不同的方法,那么我需要告诉编译器它应该使用哪个方法。我该怎么做?

我不想更改AB,也不想创建A的子类,B创建具有相同名称的方法。

我想要一种方法来告诉编译器将哪种方法用作模板的一部分,可以吗?

您可以将指向成员函数的指针作为第二个模板参数传递给D

template <class T, void (T::*fun)(C)>
struct D
{
    T t;
    void myFunct(C data)
    {
        (t.*fun)(data);
    }
};

然后,您可以通过以下方式创建D对象:

D<A, &A::write> da;
D<B, &B::use> db;

您可以使用自己的 trait 类轻松执行此操作:

template <class T>
struct DTraits;
template <>
struct DTraits<A> {
  static void call(A &obj, C data) { obj.write(data); }
};
template <>
struct DTraits<B> {
  static void call(B &obj, C data) { obj.use(data); }
};
template <class T>
struct D
{
  void myFunct(C data)
  {
    DTraits<T>::call(t, data);
  }
};

为了完整起见,这里是另一种可能的解决方案,它使用 if constexpr 、 类型特征并需要 C++17 支持:

void myFunct(C data)
{
   if constexpr (std::is_same_v<T, A>) {
       t.write(data);
   } else {
       t.use(data);
   }
}