I have two std::atomic
variables, like this:
std::atomic<bool> b1;
std::atomic<bool> b2;
At some point in the code I need to swap them. This runs before the threads are created, so I know there is only the main thread and no-one else is trying to read/write to those vars. But:
std::swap(b1, b2);
This results in:
[...] MSVC\14.24.28314\include\utility(61,1): error C2280: 'std::atomic<bool>::atomic(const std::atomic<bool> &)': attempting to reference a deleted function
[...] MSVC\14.24.28314\include\atomic(1480): message : see declaration of 'std::atomic<bool>::atomic'
[...] MSVC\14.24.28314\include\atomic(1480,5): message : 'std::atomic<bool>::atomic(const std::atomic<bool> &)': function was explicitly deleted
I'm not sure why the copy constructor is deleted. So the solution I used was to use the old-style swap with a 3rd variable:
const bool tmp = b1;
b1 = b2.load();
b2 = tmp;
But now I'm curious: Why is std::atomic
's copy constructor deleted?
(Actual code is more complicated than two single std::atomic<bool>
variables, but I tried to distill it down to a simple case for this question.)