Debian Codegraph @

libstdc++-v3/testsuite/20_util/unique_ptr/hash/abi.cc

raw
1 // { dg-do compile { target c++17 } }
2
3 #include <memory>
4
5 struct S { }; // std::hash<S> is a disabled specialization.
6
7 template<typename T, typename D = std::default_delete<T>>
8 constexpr std::size_t hash_size = sizeof(std::hash<std::unique_ptr<T, D>>);
9
10 template<typename... Ts>
11 struct MultiHash : std::hash<std::unique_ptr<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::unique_ptr<T,D >> specialization
20 // has a base class of type __hash_empty_base<D::pointer> 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 // All these types have distinct D::pointer types, so no duplicate base classes
27 static_assert( check_hash_size<1, int>, "" );
28 static_assert( check_hash_size<1, int, long, double>, "" );
29 static_assert( check_hash_size<1, int, const int>, "" );
30 static_assert( check_hash_size<1, int, long, const int>, "" );
31 // Likewise for these disabled specializations:
32 static_assert( check_hash_size<1, S>, "" );
33 static_assert( check_hash_size<1, int, S>, "" );
34 static_assert( check_hash_size<1, int, S, const int>, "" );
35 static_assert( check_hash_size<1, int, S, const int, const S>, "" );
36 static_assert( check_hash_size<1, int, S, const S, const int>, "" );
37
38 // But this has two base classes of type __hash_empty_base<int*>:
39 static_assert( check_hash_size<2, int, int[]>, "" );
40 static_assert( check_hash_size<2, int, int[], const int>, "" );
41 // And this has two base classes of type __hash_not_enabled<S*>:
42 static_assert( check_hash_size<2, S, S[]>, "" );
43 static_assert( check_hash_size<2, S, S[], const S>, "" );
44
45 struct Del : std::default_delete<int> { };
46 using P = std::unique_ptr<int>;
47 using PD = std::unique_ptr<int, Del>;
48 using PC = std::unique_ptr<int, std::default_delete<const int>>;
49 using PA = std::unique_ptr<int[]>;
50 struct HashClash
51 : std::hash<P>, std::hash<PD>, std::hash<PC>, std::hash<PA>
52 { };
53 #if _GLIBCXX_INLINE_VERSION
54 static_assert(sizeof(HashClash) == 1, "No duplicate bases for unstable ABI");
55 #else
56 static_assert(sizeof(HashClash) == 4, "four __hash_empty_base<int*> bases");
57 #endif
58
59 struct Del2 : std::default_delete<const int> { using pointer = const int*; };
60 using PD2 = std::unique_ptr<int, Del2>;
61 struct Hash2
62 : std::hash<PD>, std::hash<PD2>
63 { };
64 static_assert(sizeof(Hash2) == 1, "No duplicate bases");