Given foo.cpp
:
#include <atomic>
namespace {
std::atomic<int*> gets_zero_init;
std::atomic<int*> gets_nullptr{nullptr};
}
I'm confident that gets_zero_init
is zero-initialized. I'm also confident that gets_nullptr
will be initialized with nullptr
, I'm also fairly confident that gets_nullptr
doesn't get zero-initialized, because std::is_trivially_constructible<decltype(gets_nullptr), int*>::value
is false
(at least, it is on my compiler). I'm however a little unclear on whether gets_nullptr
is fully initialized at constant init, static init, or dynamic init. Which is it?
Furthermore, lets say, for the sake of argument, that I'm interested in ensuring that I'm using zero-initialization, so I go with the gets_zero_init
approach. The pointer embedded in gets_zero_init
will hold the all-zeros bit-pattern due to zero-initialization.
Is it guaranteed that the all-zeros bit pattern is equivalent to nullptr
? In other words, if I want the semantics of gets_nullptr
, can I rely on the zero-initialization of gets_zero_init
to provide that?
Aucun commentaire:
Enregistrer un commentaire