- I want to use std::compare_exchange_strongfor somestd::atomic<int>
- For compilation reasons (int &) I am forced to introduceint _OLD_VALUE = OLD_VALUE.
- Is there a more elegant way to achieve this?
- Here is my example
#include <atomic>
#include <stdio.h>
#define OLD_VALUE 16
#define NEW_VALUE 744
#define OTHER_VALUE 80
int main(int argc, char **argv)
{
    std::atomic<int> i(OTHER_VALUE);
    int _OLD_VALUE = OLD_VALUE;
    bool    status = i.compare_exchange_strong(_OLD_VALUE,NEW_VALUE);
    // bool status = i.compare_exchange_strong( OLD_VALUE,NEW_VALUE);
    if (status) { printf("good\n"); }
    return 0;
}
And here is the compilation error when I use the commented version:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:11:65: error: cannot bind non-const lvalue reference of type ‘std::__atomic_base<int>::__int_type& {aka int&}’ to an rvalue of type ‘int’
     bool status = i.compare_exchange_strong( OLD_VALUE,NEW_VALUE);
                                                                 ^
In file included from /usr/include/c++/7/atomic:41:0,
                 from main.cpp:1:
/usr/include/c++/7/bits/atomic_base.h:496:7: note:   initializing argument 1 of ‘bool std::__atomic_base<_IntTp>::compare_exchange_strong(std::__atomic_base<_IntTp>::__int_type&, std::__atomic_base<_IntTp>::__int_type, std::memory_order) [with _ITp = int; std::__atomic_base<_IntTp>::__int_type = int; std::memory_order = std::memory_order]’
       compare_exchange_strong(__int_type& __i1, __int_type __i2,
       ^~~~~~~~~~~~~~~~~~~~~~~
 
     
     
     
    