C++ operator 自动生成类
功能就是继承自该函数只需重写两个接口,可以自动生成所有逻辑判断运算符重载
不多bb,直接上代码,有需求可以自己更改
#include <stdio.h>
#include <stdlib.h>
#include <functional>
#include <boost/context/continuation.hpp>
using namespace boost::context::detail;
#define STACK_SIZE (4 * 1024)
namespace util
{
template<class ItA>
using GreaterThenFunc= std::function<bool(const ItA&,const ItA&)>;
template<class ItA>
using EqualFunc= std::function<bool(const ItA&,const ItA&)>;
template<class Item>
class comparator
{
public:
virtual bool operator==(const comparator<Item>& r_value_) const =0;
virtual bool operator!=(const comparator<Item>& r_value_) const
{ return !(*this == r_value_); }
virtual bool operator>(const comparator<Item>& r_value_) const = 0;
virtual bool operator<(const comparator<Item>& r_value_) const
{ return (!this->operator>(r_value_)) && (this->operator!=(r_value_));}
virtual bool operator>=(const comparator<Item>& r_value_) const
{ return (this->operator>(r_value_) || (this->operator==(r_value_))); }
virtual bool operator<=(const comparator<Item>& r_value_) const
{ return (this->operator<(r_value_) || (this->operator==(r_value_))); }
virtual const Item& GetValue() const
{
return it_;
}
virtual Item& SetValue(const Item& obj)
{
return it_=obj;
}
protected:
Item it_;
};
}
class test:public util::comparator<int>
{
public:
test(int t)
{
this->SetValue(t);
}
bool operator==(const comparator<int>& r_value_) const final
{
return it_ == r_value_.GetValue();
}
bool operator>(const comparator<int>& r_value_) const final
{
return it_>r_value_.GetValue();
}
};
void print(bool is)
{
if(is)
printf("true\n");
else
printf("false\n");
}
template<class Test>
void operatorabtest(Test&& a,Test&& b)
{
printf("%d > %d\n",a.GetValue(),b.GetValue());
print(a>b);
printf("%d < %d\n",a.GetValue(),b.GetValue());
print(a<b);
printf("%d >= %d\n",a.GetValue(),b.GetValue());
print(a>=b);
printf("%d <= %d\n",a.GetValue(),b.GetValue());
print(a<=b);
printf("%d != %d\n",a.GetValue(),b.GetValue());
print(a!=b);
printf("%d == %d\n",a.GetValue(),b.GetValue());
print(a==b);
}
int main(int argc,char** argv)
{
if(argc != 3)
{
printf("Usage: [操作数1] [操作数2]");
}
operatorabtest(test(atoi(argv[1])),test(atoi(argv[2])));
}