fork(4) download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. enum test
  9. {
  10. TEST = 2
  11. };
  12.  
  13. /*template <typename T, typename std::enable_if<std::is_enum<T>::value>::type* = nullptr>
  14. static std::size_t Hash(T const & t)
  15. {
  16.   std::cout << "Calling Hash enum" << std::endl;
  17.   return std::hash<typename std::underlying_type<T>::type>()(t);
  18. }
  19.  
  20. template <typename T, typename std::enable_if<!std::is_enum<T>::value>::type* = nullptr>
  21. static std::size_t Hash(T const & t)
  22. {
  23.   std::cout << "Calling Hash other type" << std::endl;
  24.   return std::hash<T>()(t);
  25. }*/
  26.  
  27. template <typename T>
  28. static std::size_t Hash(T const & t)
  29. {
  30. typedef typename std::conditional<std::is_enum<T>::value, std::hash<typename std::underlying_type<T>::type>, std::hash<T>>::type Hasher;
  31. return Hasher()(t);
  32. }
  33.  
  34. int main() {
  35. Hash<test>(TEST);
  36. Hash<int>(5);
  37. std::cin.get();
  38. return 0;
  39. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/5/bits/move.h:57:0,
                 from /usr/include/c++/5/bits/stl_pair.h:59,
                 from /usr/include/c++/5/bits/stl_algobase.h:64,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/5/type_traits: In instantiation of 'struct std::underlying_type<int>':
prog.cpp:30:134:   required from 'std::size_t Hash(const T&) [with T = int; std::size_t = unsigned int]'
prog.cpp:36:16:   required from here
/usr/include/c++/5/type_traits:2190:38: error: 'int' is not an enumeration type
       typedef __underlying_type(_Tp) type;
                                      ^
stdout
Standard output is empty