C++ 规范关系运算符(=,<;,…)

C++ 规范关系运算符(=,<;,…),c++,operator-overloading,codesynthesis,C++,Operator Overloading,Codesynthesis,考虑一个结构(如:几个成员的愚蠢聚合),其中所有成员都实现了某个关系R(例如是的,boost可以使用tuple来实现它) 因此,你可以通过模板自己完成。但是额外的工作似乎是在浪费时间。只需使用函数以简单的方式完成(尽管我不喜欢你的逻辑) #包括 #包括 结构X { INTA; 浮球b; }; 标准方式: #if (V == 1) // The normal way of doing it. bool operator<(X const& lhs, X const& rh

考虑一个结构(如:几个成员的愚蠢聚合),其中所有成员都实现了某个关系
R
(例如
是的,boost可以使用tuple来实现它)

因此,你可以通过模板自己完成。但是额外的工作似乎是在浪费时间。只需使用函数以简单的方式完成(尽管我不喜欢你的逻辑)

#包括
#包括
结构X
{
INTA;
浮球b;
};
标准方式:

#if (V == 1)

// The normal way of doing it.
bool operator<(X const& lhs, X const& rhs)
{
    if (lhs.a < rhs.a)                          {return true;}
    if ((lhs.a == rhs.a) && (lhs.b < rhs.b))    {return true;}

    // Of course for small structures (as above) it is easy to compress the above
    // lines into a single statement quickly.
    //
    // For larger structures they tend to break it out
    // until they get it correct then spend ten minutes
    // collapsing it into a single expression.
    return false;
}
#如果(V==1)
//这是正常的做法。

bool操作符由于显然没有非boost解决方案,我发明了一些模板魔术,我将其作为一个答案发布,以防有人有同样的问题

版本1:显式参数


return(x1.acodeconthesis
?我看不到与C公司软件的连接ode合成。正如我所说,Boost和C++11都不是一个选项。但当然,否则这将是一条路要走。
bool operator<(X const& x1, X const& x2) {
  if ((x1.a < x2.a) || (x2.a < x1.a)) // I intentionally did not use != here
    return x1.a < x2.a;
  if ((x1.b < x2.b) || (x2.b < x1.b))
    return x1.b < x2.b;
  return false;
}
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>


struct X
{
    int     a;
    float   b;
};
#if (V == 1)

// The normal way of doing it.
bool operator<(X const& lhs, X const& rhs)
{
    if (lhs.a < rhs.a)                          {return true;}
    if ((lhs.a == rhs.a) && (lhs.b < rhs.b))    {return true;}

    // Of course for small structures (as above) it is easy to compress the above
    // lines into a single statement quickly.
    //
    // For larger structures they tend to break it out
    // until they get it correct then spend ten minutes
    // collapsing it into a single expression.
    return false;
}
#elif (V == 6)

// The normal way of doing it.
bool operator<(X const& lhs, X const& rhs)
{
    return (
                (lhs.a < rhs.a)
             || ((lhs.a == rhs.a) && (lhs.b < rhs.b))
           );
}
#elif (V == 2)

// The way I like doing it because I think it is slightly more readable.
// Though I normally use the one above now.
bool operator<(X const& lhs, X const& rhs)
{
    if (lhs.a < rhs.a)      {return true;}
    if (lhs.a > rhs.a)      {return false;}
    // If we get here the A are equal
    if (lhs.b < rhs.b)      {return true;}
    if (lhs.b > rhs.b)      {return false;}
    return false;
}
#elif (V == 3)

// A version that will use tupples to do it.
bool operator<(X const& lhs, X const& rhs)
{
    typedef boost::tuple<int, float>   Comp;
    Comp    l(lhs.a, lhs.b);
    Comp    r(rhs.a, rhs.b);
    return l < r;
}
#elif (V == 4)

// A version that will use tupples but slightly more compact.
bool operator<(X const& lhs, X const& rhs)
{
    return boost::make_tuple(lhs.a, lhs.b) < boost::make_tuple(rhs.a, rhs.b);
}
#endif
namespace multioperator {

  enum LazyBool {
    LB_false = false,
    LB_true = true,
    LB_undefined
  };

  template <typename Cmp, typename B> class Operator {
    public:
      typedef typename Cmp::first_argument_type A;
    private:
      A const& a1;
      A const& a2;
      B const& b;
    public:
      Operator(A const& a1, A const& a2, B const& b)
        : a1(a1), a2(a2), b(b) {
      }
      operator bool() const {
        switch (static_cast<LazyBool>(Cmp(a1,a2))) {
          case LB_false:
            return false;
          case LB_true:
            return true;
          case LB_undefined:
          default: // g++ does not understand that we have all branches :(
            return static_cast<bool>(b);
        }
      }
  };

  template <typename Fn> class BinaryFunctorMonad {
    public:
      typedef typename Fn::first_argument_type first_argument_type;
      typedef typename Fn::second_argument_type second_argument_type;
      typedef typename Fn::result_type result_type;
    private:
      first_argument_type const& a;
      second_argument_type const& b;
    public:
      BinaryFunctorMonad(first_argument_type const& a, second_argument_type const& b)
        : a(a), b(b) {
      }
      operator result_type() {
        return Fn()(a,b);
      }
  };

  enum CmpSymmetry {
    CS_Symmetric = false,
    CS_Asymmetric = true
  };

  template <typename Cmp, CmpSymmetry asymmetric> class LazyCmp {
    public:
      typedef typename Cmp::first_argument_type first_argument_type;
      typedef typename Cmp::first_argument_type second_argument_type;
      typedef LazyBool result_type;
      LazyBool operator()(first_argument_type const& a1, second_argument_type const& a2) const {
        if (Cmp(a1,a2))
          return LB_true;
        if (asymmetric && Cmp(a2,a1))
          return LB_false;
        return LB_undefined;
      }
  };

  template <typename A, typename B> struct MultiLess {
    typedef
      Operator<
        BinaryFunctorMonad<
          LazyCmp<
            BinaryFunctorMonad<std::less<A> >,
            CS_Asymmetric>
        >, B>
      Type;
  };

  template <typename A, typename B> struct MultiEqual {
    typedef
      Operator<
        BinaryFunctorMonad<
          LazyCmp<
            BinaryFunctorMonad<std::equal_to<A> >,
            CS_Symmetric>
        >, B>
      Type;
  };

}

template <typename A, typename B> typename multioperator::MultiLess<A,B>::Type multiLess(A const& a1, A const& a2, B const& b) {
  return typename multioperator::MultiLess<A,B>::Type(a1,a2,b);
}

template <typename A, typename B> typename multioperator::MultiEqual<A,B>::Type multiEqual(A const& a1, A const& a2, B const& b) {
  return typename multioperator::MultiEqual<A,B>::Type(a1,a2,b);
}
// example: multiLess(a1,a2,multiLess(b1,b2,multiLess(c1,c2,false)))
template <typename A, typename Chain> class MultiComparable {
  private:
    A const& a;
    Chain chain;
  public:
    typedef MultiComparable MultiComparableT;
    MultiComparable(A const& a, Chain chain) : a(a), chain(chain) {}
    bool operator<(MultiComparable const& as) {
      if (a != as.a)
        return a < as.a;
      return chain < as.chain;
    }
    bool operator==(MultiComparable const& as) {
      if (a != as.a)
        return false;
      return chain == as.chain;
    }
};

template <typename A, typename Chain> MultiComparable<A,Chain> multiComparable(A const& a, Chain chain) {
  return MultiComparable<A,Chain>(a,chain);
}

//example:
struct X : MultiComparable<int,MultiComparable<float,bool> > {
  int i;
  float f;
  X() : MultiComparableT(i,multiComparable(f,false)) {}
}