c++ - 将通用模板类作为仿函数参数传递

标签 c++ c++11 stl iterator corba

我想改进以下代码片段:

THNNetIPInfoIter last = std::unique(fist, end, HNInfoIPComparator());

当前 HNInfoIPComparator() 的实现如下:

// equal comparator
class HNInfoIPComparator
{
    public:
      bool operator()(const THNNetIPInfo &a, const THNNetIPInfo &b);
      bool operator()(const SDK::TIPAddressDescription &a, const SDK::TIPAddressDescription &b);
      bool operator()(const THNNetIPInfo &a, const SDK::TIPAddressDescription &b);
      bool operator()(const SDK::TIPAddressDescription &a, const THNNetIPInfo &b);
};

此比较器定义的原因是它可能与其他 STL 算法一起使用,例如 std::set_difference 并且应该处理范围具有不同类型的情况。

问题是我必须编写大量非常相似的比较器,并且很容易纠结于使用哪个比较器。

我想写以下代码片段:

template<typename SDKClass, typename IDLClass>
class equal {
public:
  bool operator()(const IDLClass &a, const IDLClass &b) {
      if (strcmp(a.ipaddr.in(), b.ipaddr.in())) {
          return false;
      }
      return true;
  }

  bool operator()(const SDKClass &a, const SDKClass &b) {
      if (strcmp(a.ip_address().c_str(), b.ip_address().c_str())) {
          return false;
      }
      return true;
  }

  bool operator()(const IDLClass &a, const SDKClass &b) {
      if (strcmp(a.ipaddr.in(), b.ip_address().c_str())) {
          return false;
      }
      return true;
  }

  bool operator()(const SDKClass &a, const IDLClass &b) {
      if (strcmp(a.ip_address().c_str(), b.ipaddr.in())) {
          return false;
      }
      return true;
  }
};

因此,将根据 std::unique 函数内作为参数传递的类型来生成 HNInfoIPComparator()

因此我想传递给 std::unique 模板化仿函数(类)。是否可以做到这一点以及如何做到?

此外,我想处理仿函数包含一些用于比较的内部数据的情况

最重要的代码示例:

// Automatically generated structure from IDL specification
// Basically simple structure
struct  THNNetIPInfo
{     
    typedef THNNetIPInfo_var _var_type;
    typedef THNNetIPInfo_out _out_type;

    static void _tao_any_destructor (void *);
    ::TAO::String_Manager ipaddr;
    ::TAO::String_Manager netmask;
};

// THNNetIPInfoIter - class external iterator
// which was written manually
typedef Util::CorbaSeqIter<THNNetIPInfoList, THNNetIPInfo> THNNetIPInfoIter;

// THNNetIPInfoList - also automatically generated class
// from IDL specification, list of THNNetIPInfo elements
THNNetIPInfoList list(...);
THNNetIPInfoIter first(&list, 0);
THNNetIPInfoIter end(&list, list.length());

最佳答案

不要编写带有四个比较运算符的类模板,而是编写一个带有模板化比较运算符的普通类,该类将输入调整为要比较的键:

class HNInfoIPComparator {
    static const char* adapt(const THNNetIPInfo& t) {
        return t.ipaddr.in();
    }

    static const char* adapt(const SDK::TIPAddressDescription& t) {
        return t.ip_address().c_str();
    }

public:
    template <typename T, typename U>
    bool operator()(const T& t, const U& u) const {
        return !strcmp(adapt(t), adapt(u));
    }
};

您可以通过为其他类型添加 adapt 重载来轻松扩展比较器,例如 std::stringconst char*

关于c++ - 将通用模板类作为仿函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28243448/

相关文章:

c++ - boost::iostream bzip2_decompressor 不解压缩由 bzip2_compressor 压缩的文件

c++ - 使用 QTestLib 时抑制 qDebug

qt - 具有作用域枚举的 QFlags 的相等运算符

c++ - 在可变参数模板中使用垫片的更简洁的方法?

c++ - 为什么我必须把这个函数静态化

c++ - 如何在 Qt 和 QObject 的子类中使用 pimpl 习惯用法

c++ - 函数模板内的 decltype 和范围解析运算符

c++ - 将 std::unique_ptr.get() 作为参数传递给 addWidget()

C++ 11 binary_search 和 lambda 函数用例

c++ - 如何在字符串中查找包含破折号的字符串