Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/movable_box.h
35236 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___RANGES_MOVABLE_BOX_H10#define _LIBCPP___RANGES_MOVABLE_BOX_H1112#include <__concepts/constructible.h>13#include <__concepts/copyable.h>14#include <__concepts/movable.h>15#include <__config>16#include <__memory/addressof.h>17#include <__memory/construct_at.h>18#include <__type_traits/is_nothrow_constructible.h>19#include <__utility/move.h>20#include <optional>2122#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23# pragma GCC system_header24#endif2526_LIBCPP_PUSH_MACROS27#include <__undef_macros>2829_LIBCPP_BEGIN_NAMESPACE_STD3031#if _LIBCPP_STD_VER >= 203233// __movable_box allows turning a type that is move-constructible (but maybe not move-assignable) into34// a type that is both move-constructible and move-assignable. It does that by introducing an empty state35// and basically doing destroy-then-copy-construct in the assignment operator. The empty state is necessary36// to handle the case where the copy construction fails after destroying the object.37//38// In some cases, we can completely avoid the use of an empty state; we provide a specialization of39// __movable_box that does this, see below for the details.4041// until C++23, `__movable_box` was named `__copyable_box` and required the stored type to be copy-constructible, not42// just move-constructible; we preserve the old behavior in pre-C++23 modes.43template <class _Tp>44concept __movable_box_object =45# if _LIBCPP_STD_VER >= 2346move_constructible<_Tp>47# else48copy_constructible<_Tp>49# endif50&& is_object_v<_Tp>;5152namespace ranges {53// Primary template - uses std::optional and introduces an empty state in case assignment fails.54template <__movable_box_object _Tp>55class __movable_box {56_LIBCPP_NO_UNIQUE_ADDRESS optional<_Tp> __val_;5758public:59template <class... _Args>60requires is_constructible_v<_Tp, _Args...>61_LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t, _Args&&... __args) noexcept(62is_nothrow_constructible_v<_Tp, _Args...>)63: __val_(in_place, std::forward<_Args>(__args)...) {}6465_LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)66requires default_initializable<_Tp>67: __val_(in_place) {}6869_LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;70_LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&) = default;7172_LIBCPP_HIDE_FROM_ABI constexpr __movable_box&73operator=(__movable_box const& __other) noexcept(is_nothrow_copy_constructible_v<_Tp>)74# if _LIBCPP_STD_VER >= 2375requires copy_constructible<_Tp>76# endif77{78if (this != std::addressof(__other)) {79if (__other.__has_value())80__val_.emplace(*__other);81else82__val_.reset();83}84return *this;85}8687_LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)88requires movable<_Tp>89= default;9091_LIBCPP_HIDE_FROM_ABI constexpr __movable_box&92operator=(__movable_box&& __other) noexcept(is_nothrow_move_constructible_v<_Tp>) {93if (this != std::addressof(__other)) {94if (__other.__has_value())95__val_.emplace(std::move(*__other));96else97__val_.reset();98}99return *this;100}101102_LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return *__val_; }103_LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return *__val_; }104105_LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return __val_.operator->(); }106_LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return __val_.operator->(); }107108_LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return __val_.has_value(); }109};110111// This partial specialization implements an optimization for when we know we don't need to store112// an empty state to represent failure to perform an assignment. For copy-assignment, this happens:113//114// 1. If the type is copyable (which includes copy-assignment), we can use the type's own assignment operator115// directly and avoid using std::optional.116// 2. If the type is not copyable, but it is nothrow-copy-constructible, then we can implement assignment as117// destroy-and-then-construct and we know it will never fail, so we don't need an empty state.118//119// The exact same reasoning can be applied for move-assignment, with copyable replaced by movable and120// nothrow-copy-constructible replaced by nothrow-move-constructible. This specialization is enabled121// whenever we can apply any of these optimizations for both the copy assignment and the move assignment122// operator.123124# if _LIBCPP_STD_VER >= 23125template <class _Tp>126concept __doesnt_need_empty_state =127(copy_constructible<_Tp>128// 1. If copy_constructible<T> is true, movable-box<T> should store only a T if either T models129// copyable, or is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T> is true.130? copyable<_Tp> || (is_nothrow_move_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Tp>)131// 2. Otherwise, movable-box<T> should store only a T if either T models movable or132// is_nothrow_move_constructible_v<T> is true.133: movable<_Tp> || is_nothrow_move_constructible_v<_Tp>);134135// When _Tp doesn't have an assignment operator, we must implement __movable_box's assignment operator136// by doing destroy_at followed by construct_at. However, that implementation strategy leads to UB if the nested137// _Tp is potentially overlapping, as it is doing a non-transparent replacement of the sub-object, which means that138// we're not considered "nested" inside the movable-box anymore, and since we're not nested within it, [basic.life]/1.5139// says that we essentially just reused the storage of the movable-box for a completely unrelated object and ended the140// movable-box's lifetime.141// https://github.com/llvm/llvm-project/issues/70494#issuecomment-1845646490142//143// Hence, when the _Tp doesn't have an assignment operator, we can't risk making it a potentially-overlapping144// subobject because of the above, and we don't use [[no_unique_address]] in that case.145template <class _Tp>146concept __can_use_no_unique_address = (copy_constructible<_Tp> ? copyable<_Tp> : movable<_Tp>);147148# else149150template <class _Tp>151concept __doesnt_need_empty_state_for_copy = copyable<_Tp> || is_nothrow_copy_constructible_v<_Tp>;152153template <class _Tp>154concept __doesnt_need_empty_state_for_move = movable<_Tp> || is_nothrow_move_constructible_v<_Tp>;155156template <class _Tp>157concept __doesnt_need_empty_state = __doesnt_need_empty_state_for_copy<_Tp> && __doesnt_need_empty_state_for_move<_Tp>;158159template <class _Tp>160concept __can_use_no_unique_address = copyable<_Tp>;161# endif162163template <class _Tp>164struct __movable_box_holder {165_Tp __val_;166167template <class... _Args>168_LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)169: __val_(std::forward<_Args>(__args)...) {}170};171172template <class _Tp>173requires __can_use_no_unique_address<_Tp>174struct __movable_box_holder<_Tp> {175_LIBCPP_NO_UNIQUE_ADDRESS _Tp __val_;176177template <class... _Args>178_LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box_holder(in_place_t, _Args&&... __args)179: __val_(std::forward<_Args>(__args)...) {}180};181182template <__movable_box_object _Tp>183requires __doesnt_need_empty_state<_Tp>184class __movable_box<_Tp> {185_LIBCPP_NO_UNIQUE_ADDRESS __movable_box_holder<_Tp> __holder_;186187public:188template <class... _Args>189requires is_constructible_v<_Tp, _Args...>190_LIBCPP_HIDE_FROM_ABI constexpr explicit __movable_box(in_place_t __inplace, _Args&&... __args) noexcept(191is_nothrow_constructible_v<_Tp, _Args...>)192: __holder_(__inplace, std::forward<_Args>(__args)...) {}193194_LIBCPP_HIDE_FROM_ABI constexpr __movable_box() noexcept(is_nothrow_default_constructible_v<_Tp>)195requires default_initializable<_Tp>196: __holder_(in_place_t{}) {}197198_LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box const&) = default;199_LIBCPP_HIDE_FROM_ABI __movable_box(__movable_box&&) = default;200201// Implementation of assignment operators in case we perform optimization (1)202_LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box const&)203requires copyable<_Tp>204= default;205_LIBCPP_HIDE_FROM_ABI __movable_box& operator=(__movable_box&&)206requires movable<_Tp>207= default;208209// Implementation of assignment operators in case we perform optimization (2)210_LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box const& __other) noexcept {211static_assert(is_nothrow_copy_constructible_v<_Tp>);212static_assert(!__can_use_no_unique_address<_Tp>);213if (this != std::addressof(__other)) {214std::destroy_at(std::addressof(__holder_.__val_));215std::construct_at(std::addressof(__holder_.__val_), __other.__holder_.__val_);216}217return *this;218}219220_LIBCPP_HIDE_FROM_ABI constexpr __movable_box& operator=(__movable_box&& __other) noexcept {221static_assert(is_nothrow_move_constructible_v<_Tp>);222static_assert(!__can_use_no_unique_address<_Tp>);223if (this != std::addressof(__other)) {224std::destroy_at(std::addressof(__holder_.__val_));225std::construct_at(std::addressof(__holder_.__val_), std::move(__other.__holder_.__val_));226}227return *this;228}229230_LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const noexcept { return __holder_.__val_; }231_LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() noexcept { return __holder_.__val_; }232233_LIBCPP_HIDE_FROM_ABI constexpr const _Tp* operator->() const noexcept { return std::addressof(__holder_.__val_); }234_LIBCPP_HIDE_FROM_ABI constexpr _Tp* operator->() noexcept { return std::addressof(__holder_.__val_); }235236_LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const noexcept { return true; }237};238} // namespace ranges239240#endif // _LIBCPP_STD_VER >= 20241242_LIBCPP_END_NAMESPACE_STD243244_LIBCPP_POP_MACROS245246#endif // _LIBCPP___RANGES_MOVABLE_BOX_H247248249