Boost logo

Boost Users :

Subject: [Boost-users] 'Hashable' concept for type erasure
From: Samuel Christie (schrist_at_[hidden])
Date: 2014-03-20 17:53:23


Hello,

I've been trying to add a 'hashable' concept to one of my type erasure
requirements, but I haven't been getting anywhere and I still don't
understand the voluminous error messages enough to make any progress.

Here's approximately what I tried so far:

namespace mpl = boost::mpl;
namespace te = boost::type_erasure;

template<class T>
struct hashable
{
  static size_t apply(const T& obj) { return std::hash<T>()(obj); }
};

namespace std {
  template<>
  class hash<t>{
  public :
    size_t operator()(const test &t ) const
    {
      return te::call(hashable<test>(), t);
    }
  };
};

typedef mpl::vector<
  ...
  hashable<te::_self>,
  ...
> requirements

typedef any<requirements> test;

namespace std {
  template<>
  class hash<test>{
  public :
    size_t operator()(const test&h ) const
    {
      return te::call(hashable<test>(), h);
    }
  };
};

I also tried something else, similar to the following:

template<class C>
struct hashable
{
  static std::size_t apply(const C& c) { return c.hash(); }
};

namespace boost {
  namespace type_erasure {
    template<class C, class Base>
    struct concept_interface<hashable<C>, Base, C> : Base
    {
      std::size_t hash() const
      { return call(hashable<C>(), *this); }
    };
  }
}

// ... similar requirements / typedef as above

namespace std {
  template<>
  class hash<test>{
  public :
    size_t operator()(const test &t ) const
    {
      return t.hash();
    }
  };
};

The first one is probably just wrong, and for the second one I'm getting
some failed assertions when I try to use it with an implementation of hash
that is:

size_t hash() const {
    //coord is a member std::vector<int>
    return boost::hash_range(coord.begin(), coord.end());
}

Any help would be most useful.

-sc



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