Я хочу использовать unordered_map<std::pair<enum_class,other_enum_class>,std::uint8_t>
для управления некоторыми форматами пикселей.Недопустимое использование неполного типа struct std :: hash с unordered_map с std :: pair класса enum как ключ
Здесь минимальный код:
#include <unordered_map>
#include <utility>
#include <cstdint>
#include <iostream>
#include <functional>
enum class PNM : std::uint8_t { PBM, PGM, PPM };
enum class Format : bool { BIN, ASCII };
struct pair_hash {
public:
template <typename T, typename U>
std::size_t operator()(const std::pair<T, U> &x) const {
return std::hash<T>()(x.first)^std::hash<U>()(x.second);
}
};
int main(){
std::unordered_map<std::pair<PNM, Format>, std::uint8_t, pair_hash> k_magic_number ({
{ { PNM::PBM, Format::BIN }, 1 }, { { PNM::PGM, Format::BIN }, 2 }, { { PNM::PPM, Format::BIN }, 3 },
{ { PNM::PBM, Format::ASCII }, 4 }, { { PNM::PGM, Format::ASCII }, 5 }, { { PNM::PPM, Format::ASCII }, 6 }
});
std::cout << k_magic_number[std::make_pair<PNM, Format>(PNM::PBM, Format::BIN)];
}
С GCC У меня есть error, когда я пытаюсь создать экземпляр класса:
main.cpp:14:24: error: invalid use of incomplete type 'struct std::hash'
return std::hash()(x.first)^std::hash()(x.second);
In file included from
/usr/local/include/c++/5.2.0/bits/basic_string.h:5469:0,
from /usr/local/include/c++/5.2.0/string:52,
[...]
С Clang У меня также есть error:
error: implicit instantiation of undefined template 'std::hash' return std::hash()(x.first)^std::hash()(x.second); /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/bits/hashtable_policy.h:1257:16: note: in instantiation of function template specialization 'pair_hash::operator()' requested here [...]
С VS2013 У меня нет ошибки и код компилируется и выполняется.
Что не хватает в моем коде?
В зависимости от вашей потребности вы также можете рассмотреть использование std :: map, в котором отсутствует требование хэширования. – JDseca