Boost logo

Boost :

From: Joe Gottman (jgottman_at_[hidden])
Date: 2007-05-20 19:51:53


Johan Torp wrote:
> Is there any way to use boost::shared_ptrs as keys in associative
> containers and perform look up using raw pointers?
>
> What I want is something similar to this:
>
> std::map<boost::shared_ptr<int>, std::string> m;
> int i;
> m.find(&i);
> _______________________________________________

    Here's one possibility:

   struct raw_less
   {
      bool operator()(const shared_ptr<int> &lhs, const shared_ptr<int>
&rhs)
     {
        return lhs.get() < rhs.get();
     }
};

std::map<boost::shared_ptr<int>, std::string, raw_less> m;

Now m will search using raw pointers. If you really want to, you could
do something like
  int i;
   m.find(boost_shared_ptr<int>(&i, null_deleter());

where null_deleter is a class with an operator() that takes a pointer
and does nothing. However, this will be fairly useless because if i is
not owned by a shared_ptr you will never find it inside the map.

Joe Gottman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk