58 : data_(std::make_unique<T>())
62 template <
typename U = T>
64 requires(!std::same_as<std::remove_cvref_t<U>, Box>) &&
66 (!impl::IsBox<std::remove_cvref_t<U>>::value) &&
68 (std::same_as<std::remove_cvref_t<U>, T> || !std::is_constructible_v<T, Box>) &&
70 std::is_constructible_v<T, U>
72 : data_(std::make_unique<T>(std::forward<U>(arg)))
76 template <
typename... Args>
77 requires(
sizeof...(Args) >= 2) && std::is_constructible_v<T, Args...>
78 explicit Box(Args&&... args)
79 : data_(std::make_unique<T>(std::forward<Args>(args)...))
84 template <
typename Factory>
86 return Box(EmplaceFactory{}, std::forward<Factory>(factory));
89 Box(Box&& other)
noexcept =
default;
90 Box& operator=(Box&& other)
noexcept =
default;
93 : data_(std::make_unique<T>(*other))
96 Box& operator=(
const Box& other) {
102 template <
typename U = T>
103 requires(!std::same_as<std::remove_cvref_t<U>, Box>) && std::is_assignable_v<T&, U>
106 *data_ = std::forward<U>(other);
108 data_ = std::make_unique<T>(std::forward<U>(other));
114 operator
bool()
const =
delete;
116 T* operator->()
noexcept {
return Get(); }
117 const T* operator->()
const noexcept {
return Get(); }
119 T& operator*()
noexcept {
return *Get(); }
120 const T& operator*()
const noexcept {
return *Get(); }
122 bool operator==(
const Box& other)
const {
return **
this == *other; }
124 bool operator!=(
const Box& other)
const {
return **
this != *other; }
126 bool operator<(
const Box& other)
const {
return **
this < *other; }
128 bool operator>(
const Box& other)
const {
return **
this > *other; }
130 bool operator<=(
const Box& other)
const {
return **
this <= *other; }
132 bool operator>=(
const Box& other)
const {
return **
this >= *other; }
135 struct EmplaceFactory
final {};
137 template <
typename Factory>
138 explicit Box(EmplaceFactory, Factory&& factory)
139 : data_(
new T(std::forward<Factory>(factory)()))
147 const T* Get()
const noexcept {
152 std::unique_ptr<T> data_;