2

I am using struct in my project which has an unordered_map, I tried to put a struct or a pair of integers as key to the map, then manipulate the map, but couldn't compile, it says:

usr/include/c++/4.8/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type âstrruct std::hash<NAMESPACE::Span<int> >â
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;

operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
 ^
/usr/include/c++/4.8/bits/unordered_map.h:1388:5: note:   template  argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/string:48:0,

-----------------------------------------------------------
/usr/include/c++/4.8/bits/hashtable_policy.h: In instantiation of âstruct  std::__detail::_Hash_ccode_base<std::pair<int, int>, std::pair<const std::pair<int, int>, NAMESPACE::ConfigSet>, std::__detail::_Select1st, std::hash<std::pair<int, int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>â:
/usr/include/c++/4.8/bits/unordered_map.h:100:18:   required from âclass std::unordered_map<std::pair<int, int>, RTBKIT::ConfigSet>â

What's wrong? Here is my struct:

namespace MYNAMESPACE {

template<typename T>
//can pass in a list than a range for IntervalFilter
struct Span
{
    //no arg/two args not working for the template typename T
    //span(T lb, T ub);
    //span();

    T lowerBound;
    T upperBound;

    static Span
    createFromJson(const Json::Value & val); 


    void fromJson(const Json::Value & val);

    Json::Value toJson() const;

    bool empty() const;
    //didn't declare this template type, isIncluded is not used
    template<typename U> bool isIncluded(const U & value) const;

 };
 } //namespace

 #endif 

The code use the struct: ...

 private:
    static constexpr unsigned PRIORITY = 0x1401; }
    //std::unordered_map<Span<int>, ConfigSet> data;
    std::unordered_map<std::pair<int,int>, ConfigSet> data;
    void setConfig(unsigned configIndex, const AgentConfig& config) {
         auto range = [] (int first, int last) {
         return std::make_pair(first, last);
         };
         //int l = config.rangeFilter.lowerBound;
         //int r  = configrangeFilter.upperBound;
         //data[config.rangeFilter].set(configIndex);
         data[range(config.rangeFilter.lowerBound, config.rangeFilter.upperBound)].set(configIndex);
    }

};
2
  • 1
    You don't show any use of std::unordered_map in your code. Commented Apr 16, 2017 at 22:56
  • It's in another struct, see above added portion @aschepler Commented Apr 16, 2017 at 23:39

1 Answer 1

1

The problem is that the standard does not define a hash specialization for std::pair. The compiler is telling you (indirectly) that it doesn't know how to generate hash values for your keys.

You should define your own hash class and use it in your data declaration. Something like this:

struct MyHash
{
  size_t operator()(std::pair<int, int> value) const
  {
    // I make no promise that this is a good hash function for your dataset.
    return value.first + value.second;
  }
};

std::unordered_map<std::pair<int, int>, ConfigSet, MyHash> data;
Sign up to request clarification or add additional context in comments.

3 Comments

The only change I'd make is casting one of the values before the addition, as signed integer addition overflow is undefined behavior: return static_cast<ssize_t>(value.first) + value.second; Or you could do a better hash function like return (static_cast<size_t>(value.first) << sizeof(int)*8) | value.second;
It compiled, thanks !, in order to find a pair in the map by key std::pair, do I need to use/declare bool operator== for the key(struct)? Or pair already able to compare if it's the same key?
std::pair has built in comparison operators for lexicographic ordering. Its default operator== will work fine in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.