title: Why should operator< be non-member function?
body: <p>I remeber <code>C++ Primer</code> tells us <code>operator&lt;</code> should be <code>non-member function</code>, and I always obey  the rule. But now I want to know the reason.</p>
  
  <p>I wrote following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using std::cout;
  using std::endl;
  
  struct Point1
  {
    int x, y;
    Point1(const int a, const int b): x(a), y(b) { }
  };
  inline bool operator&lt;(const Point1&amp; lhs, const Point1&amp; rhs)
  {
    return lhs.x &lt; rhs.x || (lhs.x == rhs.x &amp;&amp; lhs.y &lt; rhs.y);
  }
  
  struct Point2
  {
    int x, y;
    Point2(const int a, const int b): x(a), y(b) { }
    bool operator&lt;(const Point2&amp; rhs)
    {
      return x &lt; rhs.x || (x == rhs.x &amp;&amp; y &lt; rhs.y);
    }
  };
  
  int main()
  {
    Point1 a(1, 2), b(1, 3);
    cout &lt;&lt; (a &lt; b) &lt;&lt; " " &lt;&lt; (b &lt; a) &lt;&lt; endl;
    Point2 c(2, 3), d(2, 4);
    cout &lt;&lt; (c &lt; d) &lt;&lt; " " &lt;&lt; (d &lt; c) &lt;&lt; endl;
  }
  </code></pre>
  
  <p>In this case, It seems they don't make difference and <code>member</code> function seems much simpler.</p>
  
  <p>But in this case:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using std::cout;
  using std::endl;
  
   // Usually I write it for comparing floats
  class Float1
  {
    long double _value;
  public:
    static const long double EPS = 1e-8;
    Float1(const long double value): _value(value) { }
    const long double Get() const { return _value; }
  };
  inline bool operator&lt;(const Float1&amp; lhs, const Float1&amp; rhs)
  {
    return rhs.Get() - lhs.Get() &gt; Float1::EPS;
  }
  inline bool operator&lt;(const Float1&amp; lhs, const long double rhs)
  {
    return rhs - lhs.Get() &gt; Float1::EPS;
  }
  
  class Float2
  {
    long double _value;
  public:
    static const long double EPS = 1e-8;
    Float2(const long double value): _value(value) { }
    const long double Get() const { return _value; }
    bool operator&lt;(const Float2&amp; rhs)
    {
      return rhs._value - _value &gt; Float2::EPS;
    }
    bool operator&lt;(const long double rhs)
    {
      return rhs - _value &gt; Float2::EPS;
    }
  };
  
  int main()
  {
    Float1 x(3.14);
    Float2 y(2.17);
    long double zero = .0;
    cout &lt;&lt; (x &lt; zero) &lt;&lt; " " &lt;&lt; (zero &lt; x) &lt;&lt; endl;
    //cout &lt;&lt; (y &lt; zero) &lt;&lt; " " &lt;&lt; (zero &lt; y) &lt;&lt; endl; Compile Error!
  }
  </code></pre>
  
  <p>Both (x &lt; zero) and (zero &lt; x) work! (is <code>long double</code> converted to <code>Float</code>?)</p>
  
  <p>But (zero &lt; y) don't, because zero is not a <code>Float</code>.</p>
  
  <p>You see, in first case, <code>member function</code> costs less code length, and in second case, <code>non-member function</code> makes comparing easier. So I want to know</p>
  
  <ul>
  <li>In first case, should I use <code>member function</code> instead of <code>non-member function</code>?</li>
  <li>Why <code>C++ Primer</code> suggests <code>binary operator</code>s be <code>non-member function</code>?</li>
  <li>Is there any other case that <code>member function</code> and <code>non-member function</code> make difference?</li>
  </ul>
  
  <p>Thanks for helping!</p>