| 1 |
// { dg-do run { target c++23 } } |
| 2 |
|
| 3 |
#include <memory> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
// C++23 [inout.ptr.t] Class template inout_ptr_t |
| 7 |
|
| 8 |
int counter = 0; |
| 9 |
|
| 10 |
void |
| 11 |
test_unique_ptr() |
| 12 |
{ |
| 13 |
int* ip = new int(1); |
| 14 |
std::unique_ptr<int> up(ip); |
| 15 |
{ |
| 16 |
auto iop = std::inout_ptr(up); |
| 17 |
int** ipp = iop; |
| 18 |
VERIFY( *ipp == ip ); |
| 19 |
delete *ipp; |
| 20 |
*ipp = new int(2); |
| 21 |
} |
| 22 |
VERIFY( *up == 2 ); |
| 23 |
|
| 24 |
ip = up.get(); |
| 25 |
{ |
| 26 |
std::default_delete<int> d; |
| 27 |
auto iop = std::inout_ptr(up, d); |
| 28 |
int** ipp = iop; |
| 29 |
VERIFY( *ipp == ip ); |
| 30 |
delete *ipp; |
| 31 |
*ipp = new int(3); |
| 32 |
} |
| 33 |
VERIFY( *up == 3 ); |
| 34 |
|
| 35 |
struct D |
| 36 |
{ |
| 37 |
explicit D(int id) : id(id) { } |
| 38 |
void operator()(long* p) const { ++counter; delete p; } |
| 39 |
int id; |
| 40 |
}; |
| 41 |
counter = 0; |
| 42 |
std::unique_ptr<long, D> upd(new long(3), D(11)); |
| 43 |
{ |
| 44 |
auto iop = std::inout_ptr(upd); |
| 45 |
VERIFY( counter == 0 ); |
| 46 |
long** lpp = iop; |
| 47 |
VERIFY( **lpp == 3 ); |
| 48 |
delete *lpp; |
| 49 |
*lpp = new long(4); |
| 50 |
} |
| 51 |
VERIFY( *upd == 4 ); |
| 52 |
VERIFY( upd.get_deleter().id == 11 ); |
| 53 |
VERIFY( counter == 0 ); |
| 54 |
|
| 55 |
{ |
| 56 |
D d(22); |
| 57 |
auto iop = std::inout_ptr(upd, d); |
| 58 |
VERIFY( counter == 0 ); |
| 59 |
long** lpp = iop; |
| 60 |
VERIFY( **lpp == 4 ); |
| 61 |
delete *lpp; |
| 62 |
*lpp = nullptr; |
| 63 |
} |
| 64 |
VERIFY( upd == nullptr ); |
| 65 |
VERIFY( upd.get_deleter().id == 11 ); // Deleter not replaced if p is null. |
| 66 |
VERIFY( counter == 0 ); |
| 67 |
|
| 68 |
upd.reset(new long(5)); |
| 69 |
{ |
| 70 |
D d(33); |
| 71 |
auto iop = std::inout_ptr(upd, d); |
| 72 |
VERIFY( counter == 0 ); |
| 73 |
long** lpp = iop; |
| 74 |
VERIFY( **lpp == 5 ); |
| 75 |
delete *lpp; |
| 76 |
*lpp = new long(6); |
| 77 |
} |
| 78 |
VERIFY( *upd == 6 ); |
| 79 |
VERIFY( upd.get_deleter().id == 33 ); |
| 80 |
VERIFY( counter == 0 ); |
| 81 |
|
| 82 |
struct Base { }; |
| 83 |
struct Derived : Base |
| 84 |
{ |
| 85 |
Derived(int id) : id(id) { } |
| 86 |
int id; |
| 87 |
}; |
| 88 |
std::unique_ptr<Derived> upbd(new Derived(1)); |
| 89 |
{ |
| 90 |
auto iop = std::inout_ptr<Base*>(upbd); |
| 91 |
Base** bpp = iop; |
| 92 |
VERIFY( static_cast<Derived*>(*bpp)->id == 1 ); |
| 93 |
delete (Derived*)*bpp; |
| 94 |
*bpp = new Derived(2); |
| 95 |
} |
| 96 |
VERIFY( upbd->id == 2 ); |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
test_lwg3897() |
| 101 |
{ |
| 102 |
// Verify that implementation handles LWG Issue 3897 |
| 103 |
auto nuller = [](int** p) { |
| 104 |
delete *p; |
| 105 |
*p = nullptr; |
| 106 |
}; |
| 107 |
int* i = new int{5}; |
| 108 |
nuller(std::inout_ptr(i)); |
| 109 |
|
| 110 |
VERIFY( i == nullptr ); |
| 111 |
} |
| 112 |
|
| 113 |
int main() |
| 114 |
{ |
| 115 |
test_unique_ptr(); |
| 116 |
test_lwg3897(); |
| 117 |
} |