Earlier today I stumbled over addition on std::atomic<double> not adding up to non-atomic counterpart, which turned out to be due to a std::atomic<double>
being left uninitialized (see also What's the default value for a std::atomic?). What caught my attention, though, was that my compiler did not warn me about the fact that the std::atomic<double>
was uninitialized. Consider:
#include <iostream>
#include <atomic>
int main()
{
std::atomic<double> d;
double e;
std::cout << d << " " << e << std::endl;
}
Neither clang 3.8 nor GCC 6.2 appear to be able to produce a warning for d
being uninitialized. See live at coliru: I used -Wall -pedantic -Wextra
, but got no warning both with and without -O2
. Of course a warning about e
not being initialized is typically produced. (Interestingly, gcc does not actually produce any warning when -O2
is enabled.)
Of course I cannot expect the compiler to warn me about these things; but this case left me wondering:
- Is there a particular reason why this situation is not (currently) detected/reported by the compilers in question?
- …or even (I would assume this to be unlikely): Is it impossible in general, for some reason, to detect/report the missing initialization in the specific case of
std::atomic<T>
whereT
has no default constructor? - or, maybe, is there just another command-line switch I am missing which would make the compiler produce the warning?