I want to use a unordered_map<std::pair<enum_class,other_enum_class>,std::uint8_t>
for managing some pixelmap formats.
Here the minimal code :
#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)];
}
With GCC I have an error when I try to instantiate the class :
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,
[...]
With Clang I also have an 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 [...]
With VS2013 I have no error and the code compile and executes.
What is missing in my code ?
g++-5 gives following errors:
invalid use of incomplete type
struct std::hash<PNM>
invalid use of incomplete type
struct std::hash<Format>
So, you should just specialize std::hash
for PNM
and Format
.
namespace std {
template<>
struct hash<PNM>
{
typedef PNR argument_type;
typedef size_t result_type;
result_type operator () (const argument_type& x) const
{
using type = typename std::underlying_type<argument_type>::type;
return std::hash<type>()(static_cast<type>(x));
}
};
template<>
struct hash<Format>
{
typedef Format argument_type;
typedef size_t result_type;
result_type operator () (const argument_type& x) const
{
using type = typename std::underlying_type<argument_type>::type;
return std::hash<type>()(static_cast<type>(x));
}
};
}
Or you can write template struct, that will works only for enums
with use of SFINAE (not sure, that it's not UB by standard, since it's not specialization actually).
namespace std
{
template<typename E>
struct hash
{
typedef E argument_type;
typedef size_t result_type;
using sfinae = typename std::enable_if<std::is_enum<E>::value>::type;
result_type operator() (const E& e) const
{
using base_t = typename std::underlying_type<E>::type;
return std::hash<base_t>()(static_cast<base_t>(e));
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With