explicit atomic_ref(T& obj); // (1) C++20
constexpr explicit atomic_ref(T& obj); // (1) C++26
atomic_ref(const atomic_ref& other) noexcept; // (2) C++20
constexpr atomic_ref(const atomic_ref& other) noexcept; // (2) C++26
explicit atomic_ref(T&&) = delete; // (3) C++26
template <class U>
constexpr atomic_ref(const atomic_ref<U>& other) noexcept; // (4) C++26
概要
- (1) :
objを参照して*thisにポインタとして保持する - (2) : コピーコンストラクタ。
otherが参照するオブジェクトを*thisもまた参照する - (3) : 一時オブジェクト(右辺値)を参照する
atomic_refが構築されるのを防ぐため、delete定義されている - (4) : 変換コンストラクタ。CV修飾のみが異なる
atomic_ref<U>から構築し、otherが参照するオブジェクトを*thisもまた参照する
テンプレートパラメータ制約
- (4) :
TとUが類似の型 (similar type) であり、is_convertible_v<U*, T*>がtrueであること
事前条件
- 参照するオブジェクトがメンバ定数のアライメント値
required_alignmentにアライメントされていること
例外
投げない
事後条件
- (4) :
*thisは、otherが参照しているオブジェクトを参照する
備考
- デフォルトコンストラクタは定義されない
例
基本的な使い方
#include <atomic>
int main()
{
int value = 3;
// valueを参照するatomic_refオブジェクトを構築
std::atomic_ref<int> a{value};
// コンストラクタの引数によって、
// クラステンプレートのテンプレート引数を推論 (<int>を省略)
std::atomic_ref b{value};
// cとbで同じ値 (value) を参照
std::atomic_ref c = b;
}
出力
CV修飾の異なるatomic_refへ変換する (C++26)
#include <atomic>
#include <iostream>
int main()
{
int value = 3;
std::atomic_ref<int> a{value};
// 読み取り専用のatomic_ref<const int>へ変換する
std::atomic_ref<const int> b = a;
std::cout << b.load() << std::endl;
}
出力
3
バージョン
言語
- C++20
処理系
- Clang: 9.0 ❌
- GCC: 10.1 ✅
- Visual C++: ??
参照
- LWG issue 3160.
atomic_ref() = delete;should be deleted - P3309R3
constexpr atomicandatomic_ref- C++26で
constexprに対応した
- C++26で
- LWG Issue 4472.
atomic_ref<const T>can be constructed from temporaries- C++26で、一時オブジェクト(右辺値)からの構築を禁止する
delete定義されたコンストラクタ (3) が追加された
- C++26で、一時オブジェクト(右辺値)からの構築を禁止する
- P3860R1 Proposed Resolution for NB Comment GB13-309
atomic_ref<T>is not convertible toatomic_ref<const T>