| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do compile { target c++20 } } |
| 19 |
|
| 20 |
#include <ranges> |
| 21 |
|
| 22 |
using std::ranges::__detail::__box; |
| 23 |
|
| 24 |
using T = decltype([] { return 0; }); |
| 25 |
static_assert(std::is_empty_v<__box<T>>); |
| 26 |
static_assert(std::is_nothrow_copy_constructible_v<__box<T>>); |
| 27 |
static_assert(std::is_nothrow_move_constructible_v<__box<T>>); |
| 28 |
static_assert(std::is_nothrow_constructible_v<__box<T>, std::in_place_t>); |
| 29 |
static_assert(requires (__box<T> a) { |
| 30 |
a = a; |
| 31 |
a = std::move(a); |
| 32 |
a.operator*(); |
| 33 |
a.operator->(); |
| 34 |
a.has_value(); |
| 35 |
}); |
| 36 |
|
| 37 |
struct S |
| 38 |
{ |
| 39 |
S(); |
| 40 |
~S(); |
| 41 |
S(const S&); |
| 42 |
S(S&&); |
| 43 |
S& operator=(const S&); |
| 44 |
S& operator=(S&&); |
| 45 |
}; |
| 46 |
static_assert(std::is_empty_v<__box<S>>); |
| 47 |
static_assert(!std::is_nothrow_copy_constructible_v<__box<S>> |
| 48 |
&& std::is_copy_constructible_v<__box<S>>); |
| 49 |
static_assert(!std::is_nothrow_move_constructible_v<__box<S>> |
| 50 |
&& std::is_move_constructible_v<__box<S>>); |
| 51 |
static_assert(!std::is_nothrow_constructible_v<__box<S>, std::in_place_t> |
| 52 |
&& std::is_constructible_v<__box<S>, std::in_place_t>); |
| 53 |
static_assert(requires (__box<S> a) { |
| 54 |
a = a; |
| 55 |
a = std::move(a); |
| 56 |
a.operator*(); |
| 57 |
a.operator->(); |
| 58 |
a.has_value(); |
| 59 |
}); |
| 60 |
|
| 61 |
using U = decltype([i=0] { return 0; }); |
| 62 |
static_assert(!std::is_empty_v<__box<U>>); |
| 63 |
static_assert(std::is_nothrow_copy_constructible_v<__box<U>>); |
| 64 |
static_assert(std::is_nothrow_move_constructible_v<__box<U>>); |
| 65 |
static_assert(!std::is_nothrow_constructible_v<__box<U>, std::in_place_t>); |
| 66 |
static_assert(requires (__box<U> a) { |
| 67 |
a = a; |
| 68 |
a = std::move(a); |
| 69 |
a.operator*(); |
| 70 |
a.operator->(); |
| 71 |
a.has_value(); |
| 72 |
}); |
| 73 |
|
| 74 |
constexpr bool |
| 75 |
test01() |
| 76 |
{ |
| 77 |
// Verify the default constructor value-initializes the underlying object. |
| 78 |
__box<int> x; |
| 79 |
__glibcxx_assert(*x == 0); |
| 80 |
return true; |
| 81 |
} |
| 82 |
static_assert(test01()); |
| 83 |
|
| 84 |
template<bool make_copyable> |
| 85 |
struct A { |
| 86 |
A(const A&) = default; |
| 87 |
A& operator=(const A&) requires make_copyable; |
| 88 |
A(int, int); |
| 89 |
A(std::initializer_list<int>) = delete; |
| 90 |
}; |
| 91 |
|
| 92 |
void |
| 93 |
test02() |
| 94 |
{ |
| 95 |
// PR libstdc++/100475 |
| 96 |
static_assert(std::copyable<A<true>>); |
| 97 |
__box<A<true>> x2(std::in_place, 0, 0); |
| 98 |
|
| 99 |
static_assert(!std::copyable<A<false>>); |
| 100 |
__box<A<false>> x1(std::in_place, 0, 0); |
| 101 |
} |
| 102 |
|
| 103 |
constexpr bool |
| 104 |
test03() |
| 105 |
{ |
| 106 |
// Verify correctness of the non-defaulted operator= for the partial |
| 107 |
// specialization of __box. |
| 108 |
struct B { |
| 109 |
constexpr B(int* p) : p(p) { } |
| 110 |
constexpr ~B() { ++*p; }; |
| 111 |
B(const B&) = default; |
| 112 |
B& operator=(const B&) = delete; |
| 113 |
int* p; |
| 114 |
}; |
| 115 |
static_assert(!std::copyable<B>); |
| 116 |
static_assert(std::is_nothrow_copy_constructible_v<B>); |
| 117 |
static_assert(sizeof(__box<B>) == sizeof(B)); |
| 118 |
|
| 119 |
int m = 0; |
| 120 |
__box<B> x(std::in_place, &m); |
| 121 |
__glibcxx_assert(m == 0); |
| 122 |
x = x; |
| 123 |
__glibcxx_assert(m == 0); |
| 124 |
x = std::move(x); |
| 125 |
__glibcxx_assert(m == 0); |
| 126 |
|
| 127 |
int n = 0; |
| 128 |
__box<B> y(std::in_place, &n); |
| 129 |
auto z = x; |
| 130 |
x = y; |
| 131 |
__glibcxx_assert(m == 1); |
| 132 |
__glibcxx_assert(n == 0); |
| 133 |
__glibcxx_assert(x->p == &n); |
| 134 |
__glibcxx_assert(y->p == &n); |
| 135 |
y = std::move(z); |
| 136 |
__glibcxx_assert(m == 1); |
| 137 |
__glibcxx_assert(n == 1); |
| 138 |
__glibcxx_assert(y->p == &m); |
| 139 |
__glibcxx_assert(z->p == &m); |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
static_assert(test03()); |