- [meta exposition-only]
- ranges[meta header]
- class template[meta id-type]
- cpp23[meta cpp]
movable-box は、規格の文中に現れる説明専用のクラスである。
movable-box<T> は、std::optional<T>とほとんど同じであるが、以下の差分をもつ。
テンプレートパラメーター制約 move_constructible<T> && is_object_v<T> をもつ。
Tがcopyableのモデルでない場合、コピー代入演算子は以下のように定義される。
constexpr movable-box& operator=(const movable-box& that) noexcept(is_nothrow_copy_constructible_v<T>) {
if (this != addressof(that)) {
if (that) emplace(*that);
else reset();
}
return *this;
}- movable-box[italic]
- is_nothrow_copy_constructible_v[link /reference/type_traits/is_nothrow_copy_constructible.md]
- emplace[link /reference/optional/optional/emplace.md]
- reset[link /reference/optional/optional/reset.md]
Tがmovableのモデルでない場合、ムーブ代入演算子は以下のように定義される。
constexpr movable-box& operator=(movable-box&& that) noexcept(is_nothrow_move_constructible_v<T>) {
if (this != addressof(that)) {
if (that) emplace(std::move(*that));
else reset();
}
return *this;
}- movable-box[italic]
- is_nothrow_move_constructible_v[link /reference/type_traits/is_nothrow_move_constructible.md]
- emplace[link /reference/optional/optional/emplace.md]
- reset[link /reference/optional/optional/reset.md]
以下のことが推奨される。
copy_constructible<T>なら、movable-box<T>はTがcopyableのモデルであるか、is_nothrow_move_constructible_v<T> &&is_nothrow_copy_constructible_v<T>を満たす場合のみ値を保持する。- そうでなければ、
movable-box<T>はTがmovableのモデルであるか、is_nothrow_move_constructible_v<T>を満たす場合のみ値を保持する。
このクラスは、copyable-boxを置き換える形で導入された。
- C++23