C++ operator 生成类实现

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&)>;

// typedef std::function<bool(void*)> GreaterThenFunc;
// typedef std::function<bool(void*)> EqualFunc;

/**
 * @brief 可以自动实现operator
 */
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])));

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绿色健康老清新

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值