| 1 | // { dg-do compile { target c++17 } } |
| 2 | |
| 3 | #include <optional> |
| 4 | |
| 5 | struct S { }; // std::hash<S> is a disabled specialization. |
| 6 | |
| 7 | template<typename T> |
| 8 | constexpr std::size_t hash_size = sizeof(std::hash<std::optional<T>>); |
| 9 | |
| 10 | template<typename... Ts> |
| 11 | struct MultiHash : std::hash<std::optional<Ts>>... |
| 12 | { }; |
| 13 | |
| 14 | #if _GLIBCXX_INLINE_VERSION |
| 15 | // For the unstable ABI the size should always be one. |
| 16 | template<std::size_t Size, typename... Ts> |
| 17 | constexpr bool check_hash_size = sizeof(MultiHash<Ts...>) == 1; |
| 18 | #else |
| 19 | // For the default ABI, each std::hash<std::optional<T>> specialization has |
| 20 | // a base class of type __hash_empty_base<remove_cv_t<T>> and if |
| 21 | // the same type occurs more than once they must have unique addresses. |
| 22 | template<std::size_t Size, typename... Ts> |
| 23 | constexpr bool check_hash_size = sizeof(MultiHash<Ts...>) == Size; |
| 24 | #endif |
| 25 | |
| 26 | static_assert( check_hash_size<1, int> ); |
| 27 | static_assert( check_hash_size<1, int, long, double> ); |
| 28 | static_assert( check_hash_size<2, int, const int> ); |
| 29 | static_assert( check_hash_size<2, int, long, 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, const int> ); |
| 34 | static_assert( check_hash_size<2, int, S, const int, const S> ); |
| 35 | static_assert( check_hash_size<2, int, S, const S, const int> ); |