2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___RANGES_MOVABLE_BOX_H
11 #define _LIBCPP___RANGES_MOVABLE_BOX_H
13 #include <__concepts/constructible.h>
14 #include <__concepts/copyable.h>
15 #include <__concepts/movable.h>
17 #include <__memory/addressof.h>
18 #include <__memory/construct_at.h>
19 #include <__type_traits/is_nothrow_constructible.h>
20 #include <__type_traits/is_nothrow_copy_constructible.h>
21 #include <__type_traits/is_nothrow_default_constructible.h>
22 #include <__utility/move.h>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
30 #include <__undef_macros>
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 #if _LIBCPP_STD_VER >= 20
36 // __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into
37 // a type that is both move-constructible and move-assignable. It does that by introducing an empty state
38 // and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary
39 // to handle the case where the copy construction fails after destroying the object.
41 // In some cases, we can completely avoid the use of an empty state; we provide a specialization of
42 // __movable_box that does this, see below for the details.
44 // until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not
45 // just move-constructible; we preserve the old behavior in pre-C++23 modes.
47 concept __movable_box_object
=
48 # if _LIBCPP_STD_VER >= 23
49 move_constructible
<_Tp
>
51 copy_constructible
<_Tp
>
56 // Primary template - uses std::optional and introduces an empty state in case assignment fails.
57 template <__movable_box_object _Tp
>
59 _LIBCPP_NO_UNIQUE_ADDRESS optional
<_Tp
> __val_
;
62 template <class... _Args
>
63 requires is_constructible_v
<_Tp
, _Args
...>
64 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __movable_box(in_place_t
, _Args
&&... __args
) noexcept(
65 is_nothrow_constructible_v
<_Tp
, _Args
...>)
66 : __val_(in_place
, std::forward
<_Args
>(__args
)...) {}
68 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box() noexcept(is_nothrow_default_constructible_v
<_Tp
>)
69 requires default_initializable
<_Tp
>
72 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
const&) = default;
73 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
&&) = default;
75 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
&
76 operator=(__movable_box
const& __other
) noexcept(is_nothrow_copy_constructible_v
<_Tp
>)
77 # if _LIBCPP_STD_VER >= 23
78 requires copy_constructible
<_Tp
>
81 if (this != std::addressof(__other
)) {
82 if (__other
.__has_value())
83 __val_
.emplace(*__other
);
90 _LIBCPP_HIDE_FROM_ABI __movable_box
& operator=(__movable_box
&&)
94 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
&
95 operator=(__movable_box
&& __other
) noexcept(is_nothrow_move_constructible_v
<_Tp
>) {
96 if (this != std::addressof(__other
)) {
97 if (__other
.__has_value())
98 __val_
.emplace(std::move(*__other
));
105 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
const& operator*() const noexcept
{ return *__val_
; }
106 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& operator*() noexcept
{ return *__val_
; }
108 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* operator->() const noexcept
{ return __val_
.operator->(); }
109 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* operator->() noexcept
{ return __val_
.operator->(); }
111 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_value() const noexcept
{ return __val_
.has_value(); }
114 // This partial specialization implements an optimization for when we know we don't need to store
115 // an empty state to represent failure to perform an assignment. For copy-assignment, this happens:
117 // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator
118 // directly and avoid using std::optional.
119 // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as
120 // destroy-and-then-construct and we know it will never fail, so we don't need an empty state.
122 // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and
123 // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled
124 // whenever we can apply any of these optimizations for both the copy assignment and the move assignment
127 # if _LIBCPP_STD_VER >= 23
129 concept __doesnt_need_empty_state
=
130 (copy_constructible
<_Tp
>
131 // 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models
132 // copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.
133 ? copyable
<_Tp
> || (is_nothrow_move_constructible_v
<_Tp
> && is_nothrow_copy_constructible_v
<_Tp
>)
134 // 2. Otherwise, movable-box<T> should store only a T if either T models movable or
135 // is_nothrow_move_constructible_v<T> is true.
136 : movable
<_Tp
> || is_nothrow_move_constructible_v
<_Tp
>);
140 concept __doesnt_need_empty_state_for_copy
= copyable
<_Tp
> || is_nothrow_copy_constructible_v
<_Tp
>;
143 concept __doesnt_need_empty_state_for_move
= movable
<_Tp
> || is_nothrow_move_constructible_v
<_Tp
>;
146 concept __doesnt_need_empty_state
= __doesnt_need_empty_state_for_copy
<_Tp
> && __doesnt_need_empty_state_for_move
<_Tp
>;
149 template <__movable_box_object _Tp
>
150 requires __doesnt_need_empty_state
<_Tp
>
151 class __movable_box
<_Tp
> {
152 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_
;
155 template <class... _Args
>
156 requires is_constructible_v
<_Tp
, _Args
...>
157 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __movable_box(in_place_t
, _Args
&&... __args
) noexcept(
158 is_nothrow_constructible_v
<_Tp
, _Args
...>)
159 : __val_(std::forward
<_Args
>(__args
)...) {}
161 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box() noexcept(is_nothrow_default_constructible_v
<_Tp
>)
162 requires default_initializable
<_Tp
>
165 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
const&) = default;
166 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
&&) = default;
168 // Implementation of assignment operators in case we perform optimization (1)
169 _LIBCPP_HIDE_FROM_ABI __movable_box
& operator=(__movable_box
const&)
170 requires copyable
<_Tp
>
172 _LIBCPP_HIDE_FROM_ABI __movable_box
& operator=(__movable_box
&&)
173 requires movable
<_Tp
>
176 // Implementation of assignment operators in case we perform optimization (2)
177 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
& operator=(__movable_box
const& __other
) noexcept
{
178 static_assert(is_nothrow_copy_constructible_v
<_Tp
>);
179 if (this != std::addressof(__other
)) {
180 std::destroy_at(std::addressof(__val_
));
181 std::construct_at(std::addressof(__val_
), __other
.__val_
);
186 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
& operator=(__movable_box
&& __other
) noexcept
{
187 static_assert(is_nothrow_move_constructible_v
<_Tp
>);
188 if (this != std::addressof(__other
)) {
189 std::destroy_at(std::addressof(__val_
));
190 std::construct_at(std::addressof(__val_
), std::move(__other
.__val_
));
195 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
const& operator*() const noexcept
{ return __val_
; }
196 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& operator*() noexcept
{ return __val_
; }
198 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* operator->() const noexcept
{ return std::addressof(__val_
); }
199 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* operator->() noexcept
{ return std::addressof(__val_
); }
201 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_value() const noexcept
{ return true; }
203 } // namespace ranges
205 #endif // _LIBCPP_STD_VER >= 20
207 _LIBCPP_END_NAMESPACE_STD
211 #endif // _LIBCPP___RANGES_MOVABLE_BOX_H