std::atomic_ref
<atomic>
struct atomic_ref;
The std::atomic_ref
class template applies atomic operations to the object it references.
For the lifetime of the std::atomic_ref
object, the object it references is considered an atomic object. If one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (see memory model for details on data races). In addition, accesses to atomic objects may establish inter-thread synchronization and order non-atomic memory accesses as specified by std::memory_order .
The lifetime of an object must exceed the lifetime of all std::atomic_ref
s that references the object. While any std::atomic_ref
instance referencing an object exists, the object must be exclusively accessed through these std::atomic_ref
instances. No subobject of an object referenced by an std::atomic_ref
object may be concurrently referenced by any other std::atomic_ref
object.
Atomic operations applied to an object through an std::atomic_ref
are atomic with respect to atomic operations applied through any other std::atomic_ref
referencing the same object.
Like references in the core language, constness is shallow for std::atomic_ref
- it is possible to modify the referenced value through a const std::atomic_ref
object.
If any of the following conditions are satisfied, the program is ill-formed:
- std::is_trivially_copyable_v <T> is false.
-
is_always_lock_free
is false and std::is_volatile_v <T> is true.
std::atomic_ref
is CopyConstructible.
Contents
[edit] Nested types
value_type
std::remove_cv_t <T>
difference_type
-
value_type
, ifT
is an arithmetic type other than cv bool. - Otherwise, std::ptrdiff_t , if
T
is a pointer-to-object type. - Otherwise, not defined.
[edit] Data members
ptr
the pointer to the referenced object(exposition-only member object*)
atomic_ref
(public static member constant) [edit]
[edit] Member functions
(public member function) [edit]
(public member function) [edit]
(public member function) [edit]
Provided only when T
is an arithmetic type other than cv bool or a pointer-to-object type
(public member function) [edit]
(public member function) [edit]
Provided only when T
is an integral type other than cv bool or a pointer-to-object type
(public member function) [edit]
(public member function) [edit]
Provided only when T
is an integral type other than cv bool
(public member function) [edit]
(public member function) [edit]
(public member function) [edit]
[edit] Specializations
The standard specifies that std::atomic_ref
has following specializations:
struct atomic_ref</*integral-type*/>;
struct atomic_ref</*floating-point-type*/>;
requires /* see below */
[edit] Notes
Implementations may merge the specified specializations. E.g. MSVC STL merges all of them into the primary template.
When T
is cv void or a function type, std::atomic_ref<T*> (i.e. std::atomic_ref<void*>, std::atomic_ref<int(*)()> etc.) does not have difference_type
or any operation requiring pointer arithmetic or relational comparison(since C++26).
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_atomic_ref |
201806L |
(C++20) | std::atomic_ref
|
__cpp_lib_constexpr_atomic |
202411L |
(C++26) | constexpr std::atomic and std::atomic_ref
|
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3508 (P3323R1) |
C++20 | atomic_ref<T> had unimplementable operations if T is a const type or pointer-to-non-object type
|
these operations are either constained or not provided for unsuitable T
|