我想使用 aunordered_map<std::pair<enum_class,other_enum_class>,std::uint8_t>
来管理一些像素图格式。
这是最小的代码:
#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,当我尝试实例化类时出现错误:
main.cpp:14:24:错误:无效使用不完整类型'struct std::hash'
return std::hash()(x.first) ^ std::hash()(x.second);
在来自
/usr/local/include/c++/5.2.0/bits/basic_string.h:5469:0的文件中,来自 /usr/local/include/c++/5.2.0/string:
52,
[...]
使用 Clang 我也有一个错误:
错误:未定义模板“std::hash”的隐式实例化返回 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:注意:在此处请求的函数模板特化“pair_hash::operator()”的实例化中 [...]
使用 VS2013 我没有错误,代码编译并执行。
我的代码中缺少什么?