value_type operator++() const noexcept; // (1) C++20
value_type operator++(int) const noexcept; // (2) C++20
概要
値をインクリメントする。
- (1) : 前置インクリメント
- (2) : 後置インクリメント
戻り値
以下と等価:
例外
投げない
備考
この関数は、atomic_ref
クラスの整数型およびポインタに対する特殊化で定義される。
例
基本的な使い方
#include <iostream>
#include <atomic>
int main()
{
int value = 3;
std::atomic_ref<int> x{value};
++x;
std::cout << value << std::endl;
}
出力
4
複数スレッドからインクリメントする例
#include <iostream>
#include <atomic>
#include <thread>
int main()
{
int value = 0;
// 複数スレッドでインクリメントを呼んでも、
// 最終的に全てのスレッドでのインクリメントが処理された値になる
std::thread t1 {[&value] {
++std::atomic_ref{value};
}};
std::thread t2 {[&value] {
++std::atomic_ref{value};
}};
t1.join();
t2.join();
std::cout << value << std::endl;
}
出力
2
バージョン
言語
- C++20
処理系
- Clang: 9.0 ❌
- GCC: 10.1 ✅
- Visual C++: ??
参照
- P1960R0 NB Comment Changes Reviewed by SG1
- 戻り値の型を
T
からvalue_type
に変更
- 戻り値の型を