Boost logo

Boost Users :

Subject: [Boost-users] multi_index and shared_ptrs
From: Daniel Burrows (dburrows_at_[hidden])
Date: 2009-11-05 10:32:57


  I'm having trouble getting multi_index's key extractors to play nicely
with shared_ptr objects. I'm trying to eliminate my custom hash and
equality objects in this code:

class screenshot_cache_entry : /* ... */
{
  /* ... */

public:
  const screenshot_key &get_key() const;

  /* ... */
};

class hash_cache_entry
{
public:
  size_t operator()(const boost::shared_ptr<screenshot_c
ntry> &entry) const
  {
    return boost::hash<screenshot_key>()(entry->get_key());
  }
};

class cache_entry_equal
{
public:
  bool operator()(const boost::shared_ptr<screenshot_cache_entry> &entry1,
                  const boost::shared_ptr<screenshot_cache_entry> &entry2) const
  {
    return entry1->get_key() == entry2->get_key();
  }
};

typedef multi_index_container<
  boost::shared_ptr<screenshot_cache_entry>,
  indexed_by<
    hashed_unique<tag<by_screenshot_tag>,
                  identity<boost::shared_ptr<screenshot_cache_entry> >,
                  hash_cache_entry,
                  cache_entry_equal>,
    sequenced<tag<ordered_tag> > >
> cache_map;

  Note that all my custom hash and equality comparisons do is invoke
get_key() and pass through to the corresponding operation on it. I've
been back and forth over the documentation and past mailing list posts,
and it seems like this "should" work:

typedef multi_index_container<
  boost::shared_ptr<screenshot_cache_entry>,
    indexed_by<
      hashed_unique<tag<by_screenshot_tag>,
                    const_mem_fun<screenshot_cache_entry, const screenshot_key &,
                                  &screenshot_cache_entry::get_key> >,
      sequenced<tag<ordered_tag> > >
> cache_map;

  However, when I compile this, I get an error indicating that the index
is attempting to apply hash() to the *shared pointer* rather than to
the key object (at least it didn't just hash the pointer silently!)

(...)
screenshot_cache.cc:526: instantiated from here
/usr/include/boost/multi_index/hashed_index.hpp:439: error: no match for call to ‘(const boost::hash<const aptitude::screenshot_key>) (const boost::shared_ptr<gui::<unnamed>::screenshot_cache_entry>&)’

  With the ordered index, I get a similar problem (it tries to compare
a screenshot_key to a shared_ptr).

  Can anyone shed some light on this problem?

    Thanks,
  Daniel


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net