概要
copyable-box
は、規格の文中に現れる説明専用のクラスである。
copyable-box<T>
は、std::optional<T>
とほとんど同じであるものの、
T
が copyable
のモデルであるか、is_nothrow_move_constructible_v<T> && is_nothrow_copy_constructible_v<T>
を満たす場合のみ値を保持する。
具体的には以下の差分がある。
差分1
テンプレートパラメーター制約 copy_constructible<T> && is_object_v<T>
をもつ。
差分2
T
がcopyable
のモデルでない場合、コピー代入演算子は以下のように定義される。
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;
}
差分3
T
がcopyable
のモデルでない場合、ムーブ代入演算子は以下のように定義される。
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;
}
バージョン
言語
- C++20
処理系
- Clang: 13.0.0
- GCC: 10.1.0
- ICC: ?
- Visual C++: 2019 Update 10