Arne's answer already indicates that the standard requires trivially copied types for std::atomic .
Here are some considerations why atomics may not be the best tool for your problem in the first place: Atomics are the fundamental building primitives for creating thread-safe data structures in C ++. They must be the lowest-level building blocks to build more powerful data structures, such as thread-safe containers.
In particular, atomistics is usually used to construct data structures without blocking. To lock data structures, primitives such as std::mutex and std::condition_variable are better combined, if only because it is very difficult to write a lock code with an automatics without introducing a lot of lively waiting.
Therefore, when you think of std::atomic , the first association should be blocked (despite the fact that most atomic types are technically allowed to have blocking implementations). What you are describing is a simple parallel data structure based on locking, so wrapping it with an atom should already feel wrong from a conceptual point of view.
Unfortunately, it is currently unclear how to express in language that the data structure is thread safe (which, I think, was your main intention to use atomic in the first place). Herb Sutter has some interesting ideas on this subject, but in my opinion, now we just have to admit the fact that we have to rely on the documentation to tell how certain data structures behave with respect to thread safety.
source share