Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,020 of 33,346    |
|    Ulrich Eckhardt to All    |
|    Re: why not implicit operator !=()?    |
|    16 Mar 12 06:35:43    |
   
   From: ulrich.eckhardt@dominolaser.com   
      
   Am 15.03.2012 21:52, schrieb Qi:   
   > Though logically "not ==" is almost same as "!=", the implementation   
   > maybe quite different, especially for performance.   
      
   I don't think there is a performance penalty, unless it is also in the   
   equality comparison:   
      
   // bad implementation:   
   bool operator==(T const& a, T const& b)   
   {   
    bool res = true;   
    res = (a.e1 == b.e1) and res;   
    res = (a.e2 == b.e2) and res;   
    ...   
    res = (a.eN == b.eN) and res;   
    return res;   
   }   
      
   // good implementation:   
   bool operator==(T const& a, T const& b)   
   {   
    if(a.e1 != b.e1) return false;   
    if(a.e2 != b.e2) return false;   
    ...   
    if(a.eN != b.eN) return false;   
    return true;   
   }   
      
   In other words, both equality and inequality comparison can leave the   
   function as soon as the result is clear. Note that you get the good   
   implementation automatically if you chain all comparisons using the   
   "and" operator due to short-circuit evaluation. You could also switch   
   operator order in the bad implementation to get similar results, i.e.   
   something where the compiler can skip comparisons.   
      
   Uli   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca