Generics 与成员函数调用的结果相比,最干净的方法是什么

Generics 与成员函数调用的结果相比,最干净的方法是什么,generics,boost,stl,functor,Generics,Boost,Stl,Functor,如果以前有人问过我,请原谅,我只是找不到合适的解决方案 我经常发现自己为一个类的成员函数创建函子,如下所示,用于以后的find\u if或remove\u if class by_id{ public: by_id(int id):mId(id) {} template <class T> bool operator()(T const& rX) const { return rX.getId() == mId; } template

如果以前有人问过我,请原谅,我只是找不到合适的解决方案

我经常发现自己为一个类的成员函数创建函子,如下所示,用于以后的find\u if或remove\u if

class by_id{
  public:
    by_id(int id):mId(id) {}

    template <class T>
    bool operator()(T const& rX) const { return rX.getId() == mId; }

    template <class T>
    bool operator()(T* const pX) const { return (*this)(*pX); }

  private:
    int mId;
};
class by\u id{
公众:
by_id(int id):mId(id){
模板
布尔运算符()(T const&rX)const{return rX.getId()==mId;}
模板
布尔运算符()(T*const pX)const{return(*this)(*pX);}
私人:
int mId;
};
虽然它工作得很好,但它包含了很多样板文件和方法,可以为我要用于比较的每个成员函数定义一个类

我知道C++11中的lambdas,但由于交叉编译器的限制,我无法切换到新标准

我发现的最密切相关的问题是,给定的解决方案意味着添加额外的成员函数进行比较,这很难看

难道没有更简单的方法可以使用标准STL或boost以更通用的方式编写此类函子,或者使用bind完全跳过它们吗

类似于普通函子的东西就可以了,但我缺乏编写它的技能。 我只是想说明一下我的想法:

  template<typename FP,typename COMP>
  class by_id{
      public:
        by_id(COMP id):mId(id) {}

        template <class T>
        bool operator()(T const& rX) const { return rX.FP() == mId; } 
        //of course this does not work

        template <class T>
        bool operator()(T* const pX) const { return (*this)(*pX); }

      private:
        COMP mId;
    };
模板
按id分类{
公众:
by_id(COMP id):mId(id){
模板
布尔运算符()(T const&rX)const{return rX.FP()==mId;}
//当然,这是行不通的
模板
布尔运算符()(T*const pX)const{return(*this)(*pX);}
私人:
COMP-mId;
};

如果您提前知道
T
(在创建
by\u id
对象时,您可以将其作为
by\u id
的模板参数,并将指向成员函数的指针传递给构造函数。类似于:

template<typename T, typename COMP>
class by_id
{
 public:
  typedef COMP (T::*MemFunc)();
  by_id(COMP id, MemFunc mf) : mId(id), mmf(mf) {}

  bool operator()(T const& rX) const { return rX.*mmf() == mId; }

 private:
  COMP mId;
  MemFunc mmf;
};
模板
按id分类
{
公众:
typedef COMP(T::*MemFunc)();
by_id(COMP id,MemFunc mf):mId(id),mmf(mf){
布尔运算符()(T const&rX)const{return rX.*mmf()==mId;}
私人:
COMP-mId;
MemFunc-mmf;
};

如果您提前知道
T
(在创建
by\u id
对象时,您可以将其作为
by\u id
的模板参数,并将指向成员函数的指针传递给构造函数。类似于:

template<typename T, typename COMP>
class by_id
{
 public:
  typedef COMP (T::*MemFunc)();
  by_id(COMP id, MemFunc mf) : mId(id), mmf(mf) {}

  bool operator()(T const& rX) const { return rX.*mmf() == mId; }

 private:
  COMP mId;
  MemFunc mmf;
};
模板
按id分类
{
公众:
typedef COMP(T::*MemFunc)();
by_id(COMP id,MemFunc mf):mId(id),mmf(mf){
布尔运算符()(T const&rX)const{return rX.*mmf()==mId;}
私人:
COMP-mId;
MemFunc-mmf;
};

您可以将成员函数指针传递给比较器,以指示要比较的值

使用
make\u comparer
函数避免在使用时必须指定类型参数(类似于
std::make\u pair

#包括
#包括
#包括
模板
类比较器
{
公众:
typedefr(T::*accessor_type)()常量;
类型定义R值_类型;
比较器(存取器\类型存取器,值\类型值):
存取器(存取器),值(值){}
存取器类型存取器;
值类型值;
布尔运算符()(T常量和rX)常量{
返回值(rX.*访问器41;(==值41;;
}
布尔运算符()(常数T*pX)常数{
返回(*this)(*pX);
}
};
模板
比较器生成比较器(R(T::*访问器)()常量,R值)
{
返回比较器(存取器,值);
}
福班
{
公众:
显式Foo(int-id,int-rank):id(id),rank(rank){
私人:
int-id,rank;
公众:
int id()常量{
返回id_u2;;
}
int rank()常量{
返回秩;
}
};
std::矢量foos;
typedef std::vector::const_迭代器FooIT;
无效打印(FooIT)
{
if(it==foos.end())

std::cout您可以将成员函数指针传递给比较器,以指示要比较的值

使用
make\u comparer
函数避免在使用时必须指定类型参数(类似于
std::make\u pair

#包括
#包括
#包括
模板
类比较器
{
公众:
typedefr(T::*accessor_type)()常量;
类型定义R值_类型;
比较器(存取器\类型存取器,值\类型值):
存取器(存取器),值(值){}
存取器类型存取器;
值类型值;
布尔运算符()(T常量和rX)常量{
返回值(rX.*访问器41;(==值41;;
}
布尔运算符()(常数T*pX)常数{
返回(*this)(*pX);
}
};
模板
比较器生成比较器(R(T::*访问器)()常量,R值)
{
返回比较器(存取器,值);
}
福班
{
公众:
显式Foo(int-id,int-rank):id(id),rank(rank){
私人:
int-id,rank;
公众:
int id()常量{
返回id_u2;;
}
int rank()常量{
返回秩;
}
};
std::矢量foos;
typedef std::vector::const_迭代器FooIT;
无效打印(FooIT)
{
if(it==foos.end())
标准::cout
i、 e.它测试
i.getId()
是否等于
id

在C++11中,您可以将
boost::bind
替换为
std::bind

using namespace std::placeholders;
std::find_if(first, last, std::bind(std::equal<int>(), std::bind(&Foo::getId, _1), id));
使用名称空间std::占位符;
std::find_if(第一个,最后一个,std::bind(std::equal(),std::bind(&Foo::getId,_1),id));
i、 e.它测试
i.getId()
是否等于
id

在C++11中,您可以将
boost::bind
替换为
std::bind

using namespace std::placeholders;
std::find_if(first, last, std::bind(std::equal<int>(), std::bind(&Foo::getId, _1), id));
使用名称空间std::占位符;
std::find_if(第一个,最后一个,std::bind(std::equal(),std::bind(&Foo::getId,_1),id));

再次感谢您的宝贵建议。这正是我想要的。为什么这样的东西不是某个库的一部分,它看起来很方便。再次感谢您的宝贵建议。这正是我想要的。为什么这样的东西不是某个库的一部分,它看起来很方便。
using namespace std::placeholders;
std::find_if(first, last, std::bind(std::equal<int>(), std::bind(&Foo::getId, _1), id));