| 1 | // { dg-options "-Wdeprecated" } |
| 2 | // { dg-do compile { target c++17 } } |
| 3 | |
| 4 | #include <variant> |
| 5 | |
| 6 | struct S { }; // std::hash<S> is a disabled specialization. |
| 7 | |
| 8 | // Test std::hash size |
| 9 | |
| 10 | template<typename... Ts> |
| 11 | constexpr std::size_t hash_size = sizeof(std::hash<std::variant<Ts...>>); |
| 12 | |
| 13 | #if _GLIBCXX_INLINE_VERSION |
| 14 | // For the unstable ABI the size should always be one. |
| 15 | template<std::size_t Size, typename... Ts> |
| 16 | constexpr bool check_hash_size = hash_size<Ts...> == 1; |
| 17 | #else |
| 18 | // For the default ABI, the std::hash specialization has sizeof...(Ts) |
| 19 | // base classes of types __hash_empty_base<remove_cv_t<Ts>>... and if |
| 20 | // the same type occurs more than once they must have unique addresses. |
| 21 | template<std::size_t Size, typename... Ts> |
| 22 | constexpr bool check_hash_size = hash_size<Ts...> == Size; |
| 23 | #endif |
| 24 | |
| 25 | static_assert( check_hash_size<1, int> ); |
| 26 | static_assert( check_hash_size<1, int, long, double> ); |
| 27 | static_assert( check_hash_size<2, int, long, int> ); |
| 28 | static_assert( check_hash_size<2, int, long, const int> ); |
| 29 | static_assert( check_hash_size<3, int, int, const int> ); |
| 30 | |
| 31 | static_assert( check_hash_size<1, S> ); |
| 32 | static_assert( check_hash_size<1, int, S> ); |
| 33 | static_assert( check_hash_size<2, int, S, int> ); |
| 34 | static_assert( check_hash_size<2, int, S, int, S> ); |
| 35 | static_assert( check_hash_size<2, int, S, S, int> ); |
| 36 | static_assert( check_hash_size<3, int, S, S, int, S> ); |
| 37 | |
| 38 | // For the default ABI this has two __hash_empty_base<int> base classes, |
| 39 | // for the unstable ABI it does not. |
| 40 | struct H |
| 41 | : std::hash<std::variant<int>>, std::hash<std::variant<long, int>> |
| 42 | { }; |
| 43 | static_assert( sizeof(H) == hash_size<int, int, long> ); |
| 44 | // Likewise, even though one of the base classes is a disabled specialization. |
| 45 | struct HX |
| 46 | : std::hash<std::variant<int>>, std::hash<std::variant<S, int>> |
| 47 | { }; |
| 48 | static_assert( sizeof(HX) == hash_size<int, S, int> ); |