dimanche 26 janvier 2020

C++11 atomic<>:compare_exchange_weak: why does first param have to be a non-const ref?

What is the technical reason that compare_exchange_weak(), below, cannot accept a value of 1 or iC? I can't see why it would be trying to change this variable's value, so a rvalue should be enough. Indeed it takes a rvalue for the second arg.

  #include <atomic>
  using namespace std;

  int main( int nArg, const char* apszArg[] ) {
    atomic<int> atomiA( 1 );
    int iB = 1;
    const int iC = 1;

    // Rejected
    atomiA.compare_exchange_weak( 1, 2 );

    // Accepted
    atomiA.compare_exchange_weak( iB, 2 );

    // Rejected
    atomiA.compare_exchange_weak( iC, 2 );
  }

Aucun commentaire:

Enregistrer un commentaire