Skip to content

Latest commit

 

History

History
69 lines (54 loc) · 2.59 KB

copyable_box.md

File metadata and controls

69 lines (54 loc) · 2.59 KB

copyable-box

  • ranges[meta header]
  • class template[meta id-type]
  • cpp20[meta cpp]
  • cpp23removed[meta cpp]

概要

copyable-box は、規格の文中に現れる説明専用のクラスである。

copyable-box<T> は、std::optional<T>とほとんど同じであるものの、以下の差分がある。

差分1

テンプレートパラメーター制約 copy_constructible<T> && is_object_v<T> をもつ。

差分2

Tcopyableのモデルでない場合、コピー代入演算子は以下のように定義される。

constexpr copyable-box& operator=(const copyable-box& that) noexcept(is_nothrow_copy_constructible_v<T>) {
  if (this != addressof(that)) {
    if (that) emplace(*that);
    else reset();
  }
  return *this;
}
  • copyable-box[italic]
  • is_nothrow_copy_constructible_v[link /reference/type_traits/is_nothrow_copy_constructible.md]
  • addressof[link /reference/memory/addressof.md]
  • emplace[link /reference/optional/optional/emplace.md]
  • reset[link /reference/optional/optional/reset.md]

差分3

Tcopyableのモデルでない場合、ムーブ代入演算子は以下のように定義される。

constexpr copyable-box& operator=(copyable-box&& that) noexcept(is_nothrow_move_constructible_v<T>) {
  if (this != addressof(that)) {
    if (that) emplace(std::move(*that));
    else reset();
  }
  return *this;
}
  • copyable-box[italic]
  • is_nothrow_move_constructible_v[link /reference/type_traits/is_nothrow_move_constructible.md]
  • addressof[link /reference/memory/addressof.md]
  • emplace[link /reference/optional/optional/emplace.md]
  • reset[link /reference/optional/optional/reset.md]

その他

以下のことが推奨される。

備考

このクラスはC++23でmovable-boxに置き換えられた。

バージョン

言語

  • C++20

参照