Specializations of std::atomic_ref
Explore specializations associated with std::atomic_ref.
We'll cover the following
You can specialize std::atomic_ref
for userdefined types, use partial specializations for pointer
types, or full specializations for arithmetic types such as integral or floatingpoint types.
Primary template
The primary template std::atomic_ref
can be instantiated with a TriviallyCopyable type T
as:
struct Counters {
int a;
int b;
};
Counter counter;
std::atomic_ref<Counters> cnt(counter);
Partial specializations for pointer types
The standard provides partial specializations for a pointer type: std::atomic_ref<T*>
.
Specializations for arithmetic types
The standard provides specialization for the integral and floatingpoint types: std::atomic_ref<arithmetic type>
.

Character types:
char
,char8_t(C++20)
,char16_t
,char32_t
, andwchar_t

Standard signedinteger types:
signed char
,short
,int
,long
, andlong long

Standard unsignedinteger types:
unsigned char
,unsigned short
,unsigned int
,unsigned long
, andunsigned long long

Additional integer types, defined in the header
cstdint :int8_t
,int16_t
,int32_t
, andint64_t
(signed integer with exactly $8$, $16$, $32$, and $64$ bits)uint8_t
,uint16_t
,uint32_t
, anduint64_t
(unsigned integer with exactly $8$, $16$, $32$, and $64$ bits)int_fast8_t
,int_fast16_t
,int_fast32_t
, andint_fast64_t
(fastest signed integer with at least $8$, $16$, $32$, and $64$ bits)uint_fast8_t
,uint_fast16_t
,uint_fast32_t
, anduint_fast64_t
(fastest unsigned integer with at least $8$, $16$, $32$, and $64$ bits)int_least8_t
,int_least16_t
,int_least32_t
, andint_least64_t
(smallest signed integer with at least $8$, $16$, $32$, and $64$ bits)uint_least8_t
,uint_least16_t
,uint_least32_t
, anduint_least64_t
(smallest unsigned integer with at least $8$, $16$, $32$, and $64$ bits)intmax_t
, anduintmax_t
(maximum signed and unsigned integer)intptr_t
, anduintptr_t
(signed and unsigned integer for holding a pointer)

Standard floatingpoint types:
float
,double
, andlong double
All atomic operations
First, here is the list of all operations on atomic_ref
:
Function  Description 

is_lock_free 
Checks if the atomic_ref object is lockfree. 
load 
Automatically returns the value of the referenced object. 
store 
Automatically replaces the value of the referenced object with a nonatomic. 
exchange 
Automatically replaces the value of the referenced object with the new value. 
compare_exchange_strong compare_exchange_weak 
Automatically compares and eventually exchanges the value of the referenced object. 
fetch_add, += fetch_sub, = 
Automatically adds (subtracts) the value to (from) the referenced object. 
fetch_or, = fetch_and, &= fetch_xor, ^= 
Automatically performs bitwise (AND, OR, and XOR) operation on the referenced object. 
++,  
Increments or decrements (either pre or postincrement) the referenced object. 
notify_one notify_all 
Unblocks one atomic wait operation. Unblocks all atomic wait operations. 
wait 
Blocks until it is notified. Compares itself with the old value to protect against If the value is different from the old value, returns. 
Get handson with 1200+ tech skills courses.