Let's say I have a class with a member array of std::atomic
s, where the
array is sized via a computation (i.e. it may change based on other constants elsewhere in the program):
class Foo {
static constexpr size_t kArraySize = ComputeArraySize();
std::atomic<size_t> atomics_[kArraySize];
};
What is the most elegant way to ensure that the atomics are all initialized to
zero? Can I do better than looping over the array in Foo
's constructor and
explicitly storing zero? Does the answer differ for std::array
?
Normally I would use a brace initializer here, but the derived length (which may be long) makes it difficult.
Note that I cannot assume that the instance of Foo
has static storage
duration.