The set of free functions for std::atomic_fetch_xxx
(or, and, add, sub, xor), take as input a std::atomic<T>*
named obj:
template< class T >
T atomic_fetch_sub(std::atomic<T>* obj,
typename std::atomic<T>::difference_type arg ) noexcept;
Question: Why is the std::atomic type taken as a pointer and not instead passed in as a reference eg:
T atomic_fetch_sub(std::atomic<T>& obj,
typename std::atomic<T>::difference_type arg ) noexcept;
Is there a practical rational/reason or is it simply stylistic?
The whole idea of having the free functions is to be compatible with C. You can pretty easily import C code to C++ and make it work the same there (or vice versa).
If you don't want that you can use member functions instead - using references everywhere - for example obj.fetch_sub(arg)
, or even obj -= arg
using operator overloading.
But then the code will be C++ only.