I believe the bool
itself is more problematic than std::atomic
here.
Regardless of what std::is_trivially_copyable
says for std::atomic
(which seems to be a subject of a defect report), it should be safe in practice, as long as you don't have any concurrent accesses to the same variable. Access through [unsigned] char
is exempt from strict aliasing, and I don't think there's any platform that stores anything else in std::atomic
in addition to the value it contains (as long as the type is small enough to make atomic
lock-free). There may or may not be some quirk in the standard that makes it technically illegal, but it would be a purely theoretical issue.)
A bigger problem is that if you read an invalid bit pattern into a bool
(not 0
or 1
), the code using it might break in unexpected ways.