std::atomic
has deleted copy assignment operators. Hence, the following results in a compiler error:
std::atomic<int> a1, a2;
a1 = a2; // Error
I think the motivation for the deleted operators is explained e.g. in this post. So far, so good.
But I noticed, that adding volatile
causes the code to compile suddenly (live on godbolt):
volatile std::atomic<int> a1, a2;
a1 = a2; // OK
I do not really require volatile
variables for my project, so this is just out of curiosity: Is this an oversight in the C++ standard, or is this deliberate (why?)?
Note: I can get a compiler error by hacking the std::atomic
definition, either by adding
atomic & operator=(const volatile atomic &) volatile = delete;
or by removing the conversion operator operator T() const volatile noexcept
.