Namespaces
Variants

std::atomic_ref<T>:: exchange

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
value_type exchange ( value_type desired,

std:: memory_order order =

std:: memory_order_seq_cst ) const noexcept ;
(自 C++26 起为 constexpr)

以原子操作将引用对象的值替换为 desired 。该操作属于读-改-写操作。内存访问顺序根据 order 参数值确定。

此重载仅当 std:: is_const_v < T > false 时参与重载决议。

参数

desired - 要赋的值
order - 要强制执行的内存顺序约束

返回值

调用前被引用对象的值。

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 修正后行为
LWG 3508
( P3323R1 )
C++20 exchange const T 无意义 约束为仅接受非 const T