0

Here is a Simple code to show my problem.

#include <iostream>
#include <atomic>

using namespace std;

class T {
public:
atomic<int> a;
};

int main() {
int x;
T X,Y;
x = 10;
X.a = x;
Y = X;
cout << Y.a;
}

I get the following error Message when I run this :

test.cpp: In function ‘int main()’:

test.cpp:16:5: error: use of deleted function ‘T& T::operator=(const T&)’

Y = X;
     ^

test.cpp:6:7: note: ‘T& T::operator=(const T&)’ is implicitly deleted 
because the default definition would be ill-formed:

 class T {
       ^

test.cpp:6:7: error: use of deleted function ‘std::atomic<int>& std::atomic<int>::operator=(const std::atomic<int>&)’

In file included from test.cpp:2:0:

/usr/include/c++/6/atomic:617:15: note: declared here
       atomic& operator=(const atomic&) = delete;
msc
  • 33,420
  • 29
  • 119
  • 214
  • 3
    Assignment between object of the same type (like you have with `Y = X`) is called *copy* assignment. And an [`std::atomic`](http://en.cppreference.com/w/cpp/atomic/atomic) can't be copied. – Some programmer dude Oct 12 '17 at 13:30
  • 1
    This may also be helpful: https://stackoverflow.com/questions/19961043/copy-constructor-for-classes-with-atomic-member – Retired Ninja Oct 12 '17 at 13:30
  • The error message is perfectly clear. `std::atomic` does not have an `operator=()` that accepts an argument of type `std::atomic`. Which is completely correct - the C++11 standard specifies that `operator=()` for the template type `std::atomic` is explicitly `delete`d. Since your `T` contains a `std::atomic` the compiler cannot give `T` an `operator=()`. The statement `Y = X` requires there to be an `operator=()`. – Peter Oct 12 '17 at 13:31
  • Note that in C, `struct` assignment with an `_Atomic` member will completely ignore `_Atomic` and do a regular copy of the entire struct object (unlike C++). (With no compiler warning from gcc or clang.) https://godbolt.org/g/heUd1R. – Peter Cordes Oct 13 '17 at 01:08

0 Answers0