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___CXX03___RANGES_MOVABLE_BOX_H
11 #define _LIBCPP___CXX03___RANGES_MOVABLE_BOX_H
13 #include <__cxx03/__concepts/constructible.h>
14 #include <__cxx03/__concepts/copyable.h>
15 #include <__cxx03/__concepts/movable.h>
16 #include <__cxx03/__config>
17 #include <__cxx03/__memory/addressof.h>
18 #include <__cxx03/__memory/construct_at.h>
19 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
20 #include <__cxx03/__utility/move.h>
21 #include <__cxx03/optional>
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 # pragma GCC system_header
28 #include <__cxx03/__undef_macros>
30 _LIBCPP_BEGIN_NAMESPACE_STD
32 #if _LIBCPP_STD_VER >= 20
34 // __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into
35 // a type that is both move-constructible and move-assignable. It does that by introducing an empty state
36 // and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary
37 // to handle the case where the copy construction fails after destroying the object.
39 // In some cases, we can completely avoid the use of an empty state; we provide a specialization of
40 // __movable_box that does this, see below for the details.
42 // until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not
43 // just move-constructible; we preserve the old behavior in pre-C++23 modes.
45 concept __movable_box_object
=
46 # if _LIBCPP_STD_VER >= 23
47 move_constructible
<_Tp
>
49 copy_constructible
<_Tp
>
54 // Primary template - uses std::optional and introduces an empty state in case assignment fails.
55 template <__movable_box_object _Tp
>
57 _LIBCPP_NO_UNIQUE_ADDRESS optional
<_Tp
> __val_
;
60 template <class... _Args
>
61 requires is_constructible_v
<_Tp
, _Args
...>
62 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __movable_box(in_place_t
, _Args
&&... __args
) noexcept(
63 is_nothrow_constructible_v
<_Tp
, _Args
...>)
64 : __val_(in_place
, std::forward
<_Args
>(__args
)...) {}
66 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box() noexcept(is_nothrow_default_constructible_v
<_Tp
>)
67 requires default_initializable
<_Tp
>
70 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
const&) = default;
71 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
&&) = default;
73 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
&
74 operator=(__movable_box
const& __other
) noexcept(is_nothrow_copy_constructible_v
<_Tp
>)
75 # if _LIBCPP_STD_VER >= 23
76 requires copy_constructible
<_Tp
>
79 if (this != std::addressof(__other
)) {
80 if (__other
.__has_value())
81 __val_
.emplace(*__other
);
88 _LIBCPP_HIDE_FROM_ABI __movable_box
& operator=(__movable_box
&&)
92 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
&
93 operator=(__movable_box
&& __other
) noexcept(is_nothrow_move_constructible_v
<_Tp
>) {
94 if (this != std::addressof(__other
)) {
95 if (__other
.__has_value())
96 __val_
.emplace(std::move(*__other
));
103 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
const& operator*() const noexcept
{ return *__val_
; }
104 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& operator*() noexcept
{ return *__val_
; }
106 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* operator->() const noexcept
{ return __val_
.operator->(); }
107 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* operator->() noexcept
{ return __val_
.operator->(); }
109 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_value() const noexcept
{ return __val_
.has_value(); }
112 // This partial specialization implements an optimization for when we know we don't need to store
113 // an empty state to represent failure to perform an assignment. For copy-assignment, this happens:
115 // 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator
116 // directly and avoid using std::optional.
117 // 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as
118 // destroy-and-then-construct and we know it will never fail, so we don't need an empty state.
120 // The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and
121 // nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled
122 // whenever we can apply any of these optimizations for both the copy assignment and the move assignment
125 # if _LIBCPP_STD_VER >= 23
127 concept __doesnt_need_empty_state
=
128 (copy_constructible
<_Tp
>
129 // 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models
130 // copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.
131 ? copyable
<_Tp
> || (is_nothrow_move_constructible_v
<_Tp
> && is_nothrow_copy_constructible_v
<_Tp
>)
132 // 2. Otherwise, movable-box<T> should store only a T if either T models movable or
133 // is_nothrow_move_constructible_v<T> is true.
134 : movable
<_Tp
> || is_nothrow_move_constructible_v
<_Tp
>);
136 // When _Tp doesn't have an assignment operator, we must implement __movable_box's assignment operator
137 // by doing destroy_at followed by construct_at. However, that implementation strategy leads to UB if the nested
138 // _Tp is potentially overlapping, as it is doing a non-transparent replacement of the sub-object, which means that
139 // we're not considered "nested" inside the movable-box anymore, and since we're not nested within it, [basic.life]/1.5
140 // says that we essentially just reused the storage of the movable-box for a completely unrelated object and ended the
141 // movable-box's lifetime.
142 // https://github.com/llvm/llvm-project/issues/70494#issuecomment-1845646490
144 // Hence, when the _Tp doesn't have an assignment operator, we can't risk making it a potentially-overlapping
145 // subobject because of the above, and we don't use [[no_unique_address]] in that case.
147 concept __can_use_no_unique_address
= (copy_constructible
<_Tp
> ? copyable
<_Tp
> : movable
<_Tp
>);
152 concept __doesnt_need_empty_state_for_copy
= copyable
<_Tp
> || is_nothrow_copy_constructible_v
<_Tp
>;
155 concept __doesnt_need_empty_state_for_move
= movable
<_Tp
> || is_nothrow_move_constructible_v
<_Tp
>;
158 concept __doesnt_need_empty_state
= __doesnt_need_empty_state_for_copy
<_Tp
> && __doesnt_need_empty_state_for_move
<_Tp
>;
161 concept __can_use_no_unique_address
= copyable
<_Tp
>;
165 struct __movable_box_holder
{
168 template <class... _Args
>
169 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __movable_box_holder(in_place_t
, _Args
&&... __args
)
170 : __val_(std::forward
<_Args
>(__args
)...) {}
174 requires __can_use_no_unique_address
<_Tp
>
175 struct __movable_box_holder
<_Tp
> {
176 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_
;
178 template <class... _Args
>
179 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __movable_box_holder(in_place_t
, _Args
&&... __args
)
180 : __val_(std::forward
<_Args
>(__args
)...) {}
183 template <__movable_box_object _Tp
>
184 requires __doesnt_need_empty_state
<_Tp
>
185 class __movable_box
<_Tp
> {
186 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box_holder
<_Tp
> __holder_
;
189 template <class... _Args
>
190 requires is_constructible_v
<_Tp
, _Args
...>
191 _LIBCPP_HIDE_FROM_ABI
constexpr explicit __movable_box(in_place_t __inplace
, _Args
&&... __args
) noexcept(
192 is_nothrow_constructible_v
<_Tp
, _Args
...>)
193 : __holder_(__inplace
, std::forward
<_Args
>(__args
)...) {}
195 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box() noexcept(is_nothrow_default_constructible_v
<_Tp
>)
196 requires default_initializable
<_Tp
>
197 : __holder_(in_place_t
{}) {}
199 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
const&) = default;
200 _LIBCPP_HIDE_FROM_ABI
__movable_box(__movable_box
&&) = default;
202 // Implementation of assignment operators in case we perform optimization (1)
203 _LIBCPP_HIDE_FROM_ABI __movable_box
& operator=(__movable_box
const&)
204 requires copyable
<_Tp
>
206 _LIBCPP_HIDE_FROM_ABI __movable_box
& operator=(__movable_box
&&)
207 requires movable
<_Tp
>
210 // Implementation of assignment operators in case we perform optimization (2)
211 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
& operator=(__movable_box
const& __other
) noexcept
{
212 static_assert(is_nothrow_copy_constructible_v
<_Tp
>);
213 static_assert(!__can_use_no_unique_address
<_Tp
>);
214 if (this != std::addressof(__other
)) {
215 std::destroy_at(std::addressof(__holder_
.__val_
));
216 std::construct_at(std::addressof(__holder_
.__val_
), __other
.__holder_
.__val_
);
221 _LIBCPP_HIDE_FROM_ABI
constexpr __movable_box
& operator=(__movable_box
&& __other
) noexcept
{
222 static_assert(is_nothrow_move_constructible_v
<_Tp
>);
223 static_assert(!__can_use_no_unique_address
<_Tp
>);
224 if (this != std::addressof(__other
)) {
225 std::destroy_at(std::addressof(__holder_
.__val_
));
226 std::construct_at(std::addressof(__holder_
.__val_
), std::move(__other
.__holder_
.__val_
));
231 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
const& operator*() const noexcept
{ return __holder_
.__val_
; }
232 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
& operator*() noexcept
{ return __holder_
.__val_
; }
234 _LIBCPP_HIDE_FROM_ABI
constexpr const _Tp
* operator->() const noexcept
{ return std::addressof(__holder_
.__val_
); }
235 _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
* operator->() noexcept
{ return std::addressof(__holder_
.__val_
); }
237 _LIBCPP_HIDE_FROM_ABI
constexpr bool __has_value() const noexcept
{ return true; }
239 } // namespace ranges
241 #endif // _LIBCPP_STD_VER >= 20
243 _LIBCPP_END_NAMESPACE_STD
247 #endif // _LIBCPP___CXX03___RANGES_MOVABLE_BOX_H