1

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> where T has no default constructor?
  • or, maybe, is there just another command-line switch I am missing which would make the compiler produce the warning?

1 Answer 1

0

Why: Unfortunately we have no way of knowing. Perhaps nobody who contributes to gcc development bothered to do it yet or thought it was worth it.

Impossible to detect? Not technically, I suppose. But it would have to be a special case, otherwise you'd introduce loads of warnings for a not uncommon case of declaring vars of struct and class types with no explicitly defined default constructors without initializer lists or whatever.

Worth it to add a special case? Probably, but that's a matter of opinion, and convincing somebody to implement it in gcc.

Command line switches? Not really, although -Weffc++ might get you something. I have not tried it. You could also try running it through valgrind, maybe that will report something. Also have not tried.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I tried -Weffc++, unfortunately it makes no difference in this case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.