Skip to content

Instantly share code, notes, and snippets.

@paosidufygthrj
Created December 12, 2015 15:56
Show Gist options
  • Save paosidufygthrj/9252c0281f7ea900efb1 to your computer and use it in GitHub Desktop.
Save paosidufygthrj/9252c0281f7ea900efb1 to your computer and use it in GitHub Desktop.
hash に対応しているか判定するメタ関数
#include <type_traits>
#include <functional>
using namespace std;
template <typename T, typename Ignore = void>
struct has_hash {
static const bool value = false;
};
template <typename T>
struct has_hash<T, typename enable_if<is_same<decltype(hash<T>()(T{})), std::size_t>::value>::type> {
static const bool value = true;
};
enum class my_enum {};
namespace std {
template <> struct hash<my_enum> {
size_t operator ()(my_enum) const { return 0; }
};
}
int main() {
static_assert(has_hash<my_enum>::value, "need hash function.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment