constexpr indirect& operator=(const indirect& other); // (1)
constexpr indirect& operator=(indirect&& other) noexcept(see below); // (2)
template <class U = T>
constexpr indirect& operator=(U&& u); // (3)
概要
- (1) : コピー代入。
otherが所有するオブジェクトをディープコピーする。 - (2) : ムーブ代入。
otherから所有権を移す。 - (3) : 所有するオブジェクトに値
uを代入する。
テンプレートパラメータ制約
- (3) :
remove_cvref_t<U>がindirectでなく、is_constructible_v<T, U>とis_assignable_v<T&, U>がともにtrueであること。
適格要件
- (1) :
is_copy_assignable_v<T>とis_copy_constructible_v<T>がともにtrueであること。 - (2) :
is_copy_constructible_v<T>がtrueであること。
効果
- (1) :
otherが無効値状態であれば*thisも無効値状態にする。そうでなければ、アロケータの伝播規則に従い、otherが所有するオブジェクトのコピーを*thisがもつようにする。 - (2) :
otherが無効値状態であれば*thisも無効値状態にする。そうでなければ、所有権を移すかムーブ構築する。otherは無効値状態となる。 - (3) :
*thisが無効値状態であればuからTを構築して所有する。そうでなければ**this = std::forward<U>(u)と等価。
戻り値
*thisへの参照。
例外
- (1) : 強い例外安全性を保証する。例外が送出された場合、
this->valueless_after_move()の結果は変化しない。 - (2) : 以下と等価な
noexcept指定を持つ:
noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
allocator_traits<Allocator>::is_always_equal::value)
例
#include <cassert>
#include <memory>
int main()
{
std::indirect<int> a{1};
std::indirect<int> b{2};
a = b; // (1) コピー代入(ディープコピー)
assert(*a == 2);
*a = 3;
assert(*b == 2); // bは影響を受けない
a = 10; // (3) 値の代入
assert(*a == 10);
a = std::move(b); // (2) ムーブ代入
assert(*a == 2);
assert(b.valueless_after_move());
}
出力
バージョン
言語
- C++26
処理系
- Clang: 22 ❌
- GCC: 16.1 ✅
- Visual C++: 2026 Update 2 ❌