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,699 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: "One"-to-one associations and equali   
   27 Nov 11 14:19:16   
   
   b7bfde60   
   From: daniel.kruegler@googlemail.com   
      
   Am 27.11.2011 13:59, schrieb kelvSYC:   
   > I'm having a conundrum: it seems I can't reconcile equality operators   
   > and (pseudo) one-to-one associations.  Suppose we have two classes,   
   > Key and Value, like so:   
   >   
   > struct Key {   
   >    bool operator==(const Key&  key) const;   
   >    const Value&  getValue() const;   
   > };   
   >   
   > struct Value {   
   >    boost::weak_ptr  key;   
   > };   
   >   
   > So each Value is tied to one (specific) Key among those that are   
   > equal.  The challenge is: how do I make a central Key-Value repository   
   > using C++03, such that every Value in the repository is linked to its   
   > Key?  std::map  doesn't work (I don't think, since I think   
   > the Key would be copied, and thus the Value loses its association with   
   > the Key).   
      
   You only mention ==, which is not used by std::map and would not induce a   
   strictly weak ordering (at least one that is reasonably written ;-)). In the   
   following I assume that your problem exists for your operator< or the actual   
   ordering criterion used or    
   that you will use boost's unordered containers.   
      
   > Would std::map,   
   > boost::shared_ptr>  do the trick?  Is there an easier solution?   
      
   If I understand you correctly, your criterion depends on identities of keys,   
   not of their values. Most smart pointers, like boost::shared_ptr, will satisfy   
   this. But any other identity wrapper will do so as well. In theory you could   
   use boost::reference_   
   wrapper, for example. But this means that you need to take care for the   
   life-cycle management of the referenced keys "manually". If you can ensure   
   that the keys have a life-time that is not shorter than that of the container,   
   there should be no problem    
   with using something like boost::reference_wrapper.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
   --   
         [ 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