home bbs files messages ]

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 31,725 of 33,346   
   Nevin ":-]" Liber to Arne Mertz   
   Re: Specializing std::less without an op   
   08 Dec 11 09:23:32   
   
   b30d65d8   
   From: nevin@eviloverlord.com   
      
   In article   
   <8baf8be7-9bf4-4a39-9ae8-6ffecf468065@q11g2000vbq.googlegroups.com>,   
    Arne Mertz  wrote:   
      
   > I would consider having an op< without corresponding op> etc. even   
   > more   
   > confusing than having the full set of meaningless comparison   
   > operators.   
      
   Why?  If the actual ordering is meaningless, I really don't expect   
   anyone to be explicitly doing the comparison.  operator< covers all the   
   standard containers and algorithms.   
      
   > The same applies to op< - if you want a means of comparison for the   
   > composing class you have to define it, be its name std::less or op<   
   > or some custom Functor.   
      
   I want it to be easy to define those things, because if it isn't easy,   
   people either aren't going to do it, or they will get it wrong.   
      
   If I use operator<, I can do things like   
      
   struct W   
   {   
       X x;   
       Y y;   
       Z z;   
      
       friend bool operator<(W const& lhs, W const& rhs)   
       { return std::tie(lhs.x, lhs.y, lhs.z) < std::tie(rhs.x, rhs.y,   
   rhs.z); }   
   };   
      
   Compared with:   
      
   struct W   
   {   
       X x;   
       Y y;   
       Z z;   
   };   
      
   namespace std   
   {   
      template<>   
      struct less   
      {   
         typedef W first_argument_type;   
         typedef W second_argument_type;   
         typedef bool result_type;   
      
         bool operator()(W const& lhs, W const& rhs) const   
         {   
             if (std::less()(lhs.x, rhs.x)) return true;   
             if (std::less()(rhs.x, lhs.x)) return false;   
             if (std::less()(lhs.y, rhs.y)) return true;   
             if (std::less()(rhs.x, lhs.y)) return false;   
             return std::less(lhs.z)()(lhs.z, rhs.z);   
         }   
      };   
      
      // Don't forget to write the other comparison function objects   
   }   
      
   I know which one I'd rather write...   
      
   --   
   Nevin ":-)" Liber     
      
      
         [ 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