T
operator=(T desired) const noexcept; // (1) C++20
constexpr value_type
operator=(value_type desired) const noexcept; // (1) C++26
概要
値を書き込む
テンプレートパラメータ制約
- C++26 :
is_const_v<T>
がfalse
であること
効果
以下と等価:
store(desired);
return desired;
例外
投げない
備考
- 参照先の変更ではなく、参照先の値を書き換えるので注意
例
#include <iostream>
#include <atomic>
int main()
{
int value = 3;
std::atomic_ref<int> x{value};
x = 2;
std::cout << value << std::endl;
}
出力
2
バージョン
言語
- C++20
処理系
- Clang: 9.0 ❌
- GCC: 10.1 ✅
- Visual C++: ??
参照
- P1960R0 NB Comment Changes Reviewed by SG1
- 宣言に
const
を追加
- 宣言に
- P3323R1 cv-qualified types in
atomic
andatomic_ref
- C++26でCV修飾されたテンプレート引数を受け取れるようになった
- P3309R3
constexpr atomic
andatomic_ref
- C++26で
constexpr
に対応した
- C++26で