| 1 |
// Verify P2321R2 "zip" enhancements to std::pair. |
| 2 |
// { dg-do run { target c++23 } } |
| 3 |
|
| 4 |
#include <utility> |
| 5 |
#include <testsuite_hooks.h> |
| 6 |
|
| 7 |
using std::pair; |
| 8 |
|
| 9 |
struct A { }; |
| 10 |
|
| 11 |
constexpr bool |
| 12 |
test01() |
| 13 |
{ |
| 14 |
struct B { bool v; constexpr B(A&) : v(true) { } }; |
| 15 |
|
| 16 |
// template<class U1, class U2> |
| 17 |
// constexpr explicit(false) pair(pair<U1, U2>&); |
| 18 |
|
| 19 |
pair<A, int> p2a0; |
| 20 |
pair<B, int> p2b0 = p2a0; |
| 21 |
VERIFY( std::get<0>(p2b0).v ); |
| 22 |
|
| 23 |
pair<int, A> p2a1; |
| 24 |
pair<int, B> p2b1 = p2a1; |
| 25 |
VERIFY( std::get<1>(p2b1).v ); |
| 26 |
|
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
constexpr bool |
| 31 |
test02() |
| 32 |
{ |
| 33 |
struct B { bool v; explicit constexpr B(A&) : v(true) { } }; |
| 34 |
|
| 35 |
// template<class U1, class U2> |
| 36 |
// constexpr explicit(true) pair(pair<U1, U2>&); |
| 37 |
|
| 38 |
static_assert(!std::is_convertible_v<pair<A, int>&, pair<B, int>>); |
| 39 |
static_assert(!std::is_convertible_v<pair<int, A>&, pair<int, B>>); |
| 40 |
|
| 41 |
pair<A, int> p2a0; |
| 42 |
pair<B, int> p2b0(p2a0); |
| 43 |
VERIFY( std::get<0>(p2b0).v ); |
| 44 |
|
| 45 |
pair<int, A> p2a1; |
| 46 |
pair<int, B> p2b1(p2a1); |
| 47 |
VERIFY( std::get<1>(p2b1).v ); |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
constexpr bool |
| 53 |
test03() |
| 54 |
{ |
| 55 |
struct B { bool v; constexpr B(const A&&) : v(true) { } }; |
| 56 |
|
| 57 |
// template<class U1, class U2> |
| 58 |
// constexpr explicit(false) pair(const pair<U1, U2>&&); |
| 59 |
|
| 60 |
const pair<A, int> p2a0; |
| 61 |
pair<B, int> p2b0 = std::move(p2a0); |
| 62 |
VERIFY( std::get<0>(p2b0).v ); |
| 63 |
|
| 64 |
const pair<int, A> p2a1; |
| 65 |
pair<int, B> p2b1 = std::move(p2a1); |
| 66 |
VERIFY( std::get<1>(p2b1).v ); |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
constexpr bool |
| 72 |
test04() |
| 73 |
{ |
| 74 |
struct B { bool v; explicit constexpr B(const A&&) : v(true) { } }; |
| 75 |
|
| 76 |
// template<class U1, class U2> |
| 77 |
// constexpr explicit(true) pair(const pair<U1, U2>&&); |
| 78 |
|
| 79 |
static_assert(!std::is_convertible_v<const pair<A, int>&&, pair<B, int>>); |
| 80 |
static_assert(!std::is_convertible_v<const pair<int, A>&&, pair<int, B>>); |
| 81 |
|
| 82 |
const pair<A, int> p2a0; |
| 83 |
pair<B, int> p2b0(std::move(p2a0)); |
| 84 |
VERIFY( std::get<0>(p2b0).v ); |
| 85 |
|
| 86 |
const pair<int, A> p2a1; |
| 87 |
pair<int, B> p2b1(std::move(p2a1)); |
| 88 |
VERIFY( std::get<1>(p2b1).v ); |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
constexpr bool |
| 94 |
test05() |
| 95 |
{ |
| 96 |
struct B |
| 97 |
{ |
| 98 |
mutable bool v; |
| 99 |
constexpr const B& operator=(const A&) const { v = true; return *this; } |
| 100 |
}; |
| 101 |
|
| 102 |
// template<class U1, class U2> |
| 103 |
// constexpr const pair& operator=(const pair<U1, U2>&) const; |
| 104 |
|
| 105 |
const pair<A, A> p2a; |
| 106 |
const pair<B, B> p2b; |
| 107 |
p2b = p2a; |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
constexpr bool |
| 113 |
test06() |
| 114 |
{ |
| 115 |
struct B |
| 116 |
{ |
| 117 |
mutable bool v; |
| 118 |
constexpr const B& operator=(A&&) const { v = true; return *this; } |
| 119 |
}; |
| 120 |
|
| 121 |
// template<class U1, class U2> |
| 122 |
// constexpr const pair& operator=(pair<U1, U2>&&) const; |
| 123 |
|
| 124 |
pair<A, A> p2a; |
| 125 |
const pair<B, B> p2b; |
| 126 |
p2b = std::move(p2a); |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
constexpr bool |
| 132 |
test07() |
| 133 |
{ |
| 134 |
struct B |
| 135 |
{ |
| 136 |
mutable bool v; |
| 137 |
constexpr const B& operator=(const B&) const { v = true; return *this; } |
| 138 |
}; |
| 139 |
|
| 140 |
// constexpr const pair& operator=(const pair&) const; |
| 141 |
|
| 142 |
const pair<B, B> t2a; |
| 143 |
const pair<B, B> t2b; |
| 144 |
t2b = t2a; |
| 145 |
VERIFY( std::get<0>(t2b).v ); |
| 146 |
VERIFY( std::get<1>(t2b).v ); |
| 147 |
|
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
constexpr bool |
| 152 |
test08() |
| 153 |
{ |
| 154 |
struct B |
| 155 |
{ |
| 156 |
mutable bool v; |
| 157 |
constexpr const B& operator=(B&&) const { v = true; return *this; } |
| 158 |
}; |
| 159 |
|
| 160 |
// constexpr const pair& operator=(pair&&) const; |
| 161 |
|
| 162 |
pair<B, B> t2a; |
| 163 |
const pair<B, B> t2b; |
| 164 |
t2b = std::move(t2a); |
| 165 |
VERIFY( std::get<0>(t2b).v ); |
| 166 |
VERIFY( std::get<1>(t2b).v ); |
| 167 |
|
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
struct S |
| 172 |
{ |
| 173 |
mutable int v = 0; |
| 174 |
friend constexpr void swap(S&& x, S&& y) = delete; |
| 175 |
friend constexpr void swap(const S& x, const S& y) { ++x.v; ++y.v; } |
| 176 |
}; |
| 177 |
|
| 178 |
constexpr bool |
| 179 |
test09() |
| 180 |
{ |
| 181 |
const pair<S, S> t2, u2; |
| 182 |
std::swap(t2, u2); |
| 183 |
VERIFY( std::get<0>(t2).v == 1 ); |
| 184 |
VERIFY( std::get<0>(u2).v == 1 ); |
| 185 |
VERIFY( std::get<1>(t2).v == 1 ); |
| 186 |
VERIFY( std::get<1>(u2).v == 1 ); |
| 187 |
|
| 188 |
static_assert(!std::is_swappable_v<const pair<A, int>&>); |
| 189 |
static_assert(!std::is_swappable_v<const pair<int, A>&>); |
| 190 |
|
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
int |
| 195 |
main() |
| 196 |
{ |
| 197 |
static_assert(test01()); |
| 198 |
static_assert(test02()); |
| 199 |
static_assert(test03()); |
| 200 |
static_assert(test04()); |
| 201 |
// FIXME: G++ doesn't support reading mutable members during constexpr (PR c++/92505). |
| 202 |
test05(); |
| 203 |
test06(); |
| 204 |
test07(); |
| 205 |
test08(); |
| 206 |
test09(); |
| 207 |
} |