GCC's 'maybe unitialized' warning
Lets talk about GCC’s
So I have this code:
double v; p.Get(k, v); SetValue(Clamp(v, 0.0, 1.0));
It works great. With optimisations disabled, it compiles nicely. With optimisations enabled, the SetValue line warns:
Gauge.cpp:113:36: warning: ‘v’ may be used uninitialized in this function [-Wmaybe-uninitialized]
Naively I feel like the warning shouldn’t be there. Everything here is visible to the compiler in this compilation unit. But its ok, its compiler saying “this might be broken”. That’s nice, that’s useful.
Except that there’s really no good way to silence the warning and nothing else. Yes, we can initialise
0.0. But that’s adding code.
There is a shit way to silence the warning:
#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" SetValue(Clamp(v, 0.0, 1.0)); #pragma GCC diagnostic pop
Oh, but we have to compile with multiple compilers, and some aren’t great at quietly ignoring unknown pragmas. So we actually end up with:
#ifdef __GNUC__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif SetValue(Clamp(v, 0.0, 1.0)); #ifdef __GNUC__ #pragma GCC diagnostic pop #endif
At every point this occurs. Which is at least five on my current GCC 4.8, and more in 4.7.
I could perhaps macro something up to hide all that junk away, but its going to get pretty unwieldy.
So I will explcitly initialise them, even though they don’t need it, and even though they add more code. Its has negligible overhead, so its not actually a problem, its just annoying.