std::unordered_map Ошибка в объявлении

В Execution.cppМне нужно добавить unordered_map, Я использовал следующую инструкцию:

#include <unordered_map>

std::unordered_map<StringRef, std::unordered_map<StringRef, struct IntRel>> BinaryRel;

но это вызывает следующие ошибки:

/usr/include/c++/4.8/bits/functional_hash.h:58:12: error: declaration of ‘struct std::hash<llvm::StringRef>’
    struct hash;

/usr/include/c++/4.8/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type ‘struct std::hash<llvm::StringRef>’
    using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;

1 ответ

Вы должны специализироваться std::hash для тебя StringRef тип. Например:

#include <unordered_map>

struct StringRef {};
struct IntRel {};

namespace std {
    template<>
    struct hash<StringRef>
    {
      std::size_t operator()(const StringRef& s) const noexcept { return 0; }  
    };
}

int main()
{
    std::unordered_map<StringRef, std::unordered_map<StringRef, struct IntRel>> BinaryRel;

}

Хотя я предлагаю лучшую реализацию функции хеширования:D Для этого вы можете использовать одну из существующих специализаций (std::hash<std::string>? если твой StringRef возможно содержит std::string объект).

Другие вопросы по тегам