| 1 |
// { dg-do compile { target c++23 } } |
| 2 |
// { dg-add-options no_pch } |
| 3 |
|
| 4 |
#include <memory> |
| 5 |
|
| 6 |
#ifndef __cpp_lib_constexpr_memory |
| 7 |
# error "Feature test macro for constexpr unique_ptr is missing in <memory>" |
| 8 |
#elif __cpp_lib_constexpr_memory < 202202L |
| 9 |
# error "Feature test macro for constexpr unique_ptr has wrong value in <memory>" |
| 10 |
#endif |
| 11 |
|
| 12 |
#include <testsuite_hooks.h> |
| 13 |
|
| 14 |
constexpr bool |
| 15 |
test_default() |
| 16 |
{ |
| 17 |
std::unique_ptr<int> p; |
| 18 |
std::unique_ptr<int> np(nullptr); |
| 19 |
VERIFY( p == np ); |
| 20 |
|
| 21 |
std::unique_ptr<int[]> a; |
| 22 |
std::unique_ptr<int[]> na(nullptr); |
| 23 |
VERIFY( a == na ); |
| 24 |
|
| 25 |
return true; |
| 26 |
} |
| 27 |
static_assert( test_default() ); |
| 28 |
|
| 29 |
constexpr bool |
| 30 |
test_ptr() |
| 31 |
{ |
| 32 |
std::unique_ptr<int> p(new int(2)); |
| 33 |
VERIFY( *p == 2 ); |
| 34 |
std::unique_ptr<int[]> a(new int[]{0, 1, 2}); |
| 35 |
VERIFY( a[2] == 2 ); |
| 36 |
|
| 37 |
return true; |
| 38 |
} |
| 39 |
static_assert( test_ptr() ); |
| 40 |
|
| 41 |
constexpr bool |
| 42 |
test_del() |
| 43 |
{ |
| 44 |
const std::default_delete<int> pd; |
| 45 |
std::unique_ptr<int> p1(new int(1), pd); |
| 46 |
VERIFY( *p1 == 1 ); |
| 47 |
std::unique_ptr<int> p2(new int(2), std::default_delete<int>{}); |
| 48 |
VERIFY( *p2 == 2 ); |
| 49 |
const std::default_delete<int[]> ad; |
| 50 |
std::unique_ptr<int[]> a1(new int[]{3, 4}, ad); |
| 51 |
VERIFY( a1[0] == 3 ); |
| 52 |
std::unique_ptr<int[]> a2(new int[]{5, 6}, std::default_delete<int[]>{}); |
| 53 |
VERIFY( a2[1] == 6 ); |
| 54 |
|
| 55 |
return true; |
| 56 |
} |
| 57 |
static_assert( test_del() ); |
| 58 |
|
| 59 |
constexpr bool |
| 60 |
test_move() |
| 61 |
{ |
| 62 |
std::unique_ptr<int> p1(new int(2)); |
| 63 |
std::unique_ptr<int> p2 = std::move(p1); |
| 64 |
VERIFY( *p2 == 2 ); |
| 65 |
std::unique_ptr<int[]> a1(new int[]{0, 1, 2}); |
| 66 |
std::unique_ptr<int[]> a2 = std::move(a1); |
| 67 |
VERIFY( a2[2] == 2 ); |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
static_assert( test_move() ); |
| 72 |
|
| 73 |
constexpr bool |
| 74 |
test_convert() |
| 75 |
{ |
| 76 |
std::unique_ptr<int> p1(new int(2)); |
| 77 |
std::unique_ptr<const int> p2 = std::move(p1); |
| 78 |
VERIFY( *p2 == 2 ); |
| 79 |
std::unique_ptr<int[]> a1(new int[]{0, 1, 2}); |
| 80 |
std::unique_ptr<const int[]> a2 = std::move(a1); |
| 81 |
VERIFY( a2[2] == 2 ); |
| 82 |
|
| 83 |
return true; |
| 84 |
} |
| 85 |
static_assert( test_convert() ); |