1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // UNSUPPORTED: c++98, c++03, c++11 11 12 // <functional> 13 14 // make sure that we can hash enumeration values 15 // Not very portable 16 17 #include "test_macros.h" 18 19 #include <functional> 20 #include <cassert> 21 #include <type_traits> 22 #include <limits> 23 24 enum class Colors { red, orange, yellow, green, blue, indigo, violet }; 25 enum class Cardinals { zero, one, two, three, five=5 }; 26 enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet }; 27 enum class ShortColors : long { red, orange, yellow, green, blue, indigo, violet }; 28 enum class EightBitColors : uint8_t { red, orange, yellow, green, blue, indigo, violet }; 29 30 enum Fruits { apple, pear, grape, mango, cantaloupe }; 31 32 template <class T> 33 void 34 test() 35 { 36 typedef std::hash<T> H; 37 static_assert((std::is_same<typename H::argument_type, T>::value), "" ); 38 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); 39 typedef typename std::underlying_type<T>::type under_type; 40 41 H h1; 42 std::hash<under_type> h2; 43 for (int i = 0; i <= 5; ++i) 44 { 45 T t(static_cast<T> (i)); 46 const bool small = std::integral_constant<bool, sizeof(T) <= sizeof(std::size_t)>::value; // avoid compiler warnings 47 if (small) 48 assert(h1(t) == h2(static_cast<under_type>(i))); 49 } 50 } 51 52 int main() 53 { 54 test<Cardinals>(); 55 56 test<Colors>(); 57 test<ShortColors>(); 58 test<LongColors>(); 59 test<EightBitColors>(); 60 61 test<Fruits>(); 62 } 63