https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108374
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Romain Geissler from comment #0) > std::weak_ptr<A> weakPointer(pointer); > > [[maybe_unused]] const unsigned int aAttr = weakPointer.lock()->_attr; If pointer == nullptr then weakPointer.lock() is also null, and so dereferencing it to access the attr member is undefined, and does indeed perform an atomic load at address 0. Instead of complaining about it, I would expect GCC to treat that undefined condition as unreachable and optimize it away.