I am about to release a code and I want no warnings in it.
Currently running with -Wall -Wextra
to hunt down everything I can.
Now I have this example code (extremely contrived from the production code):
// Type your code here, or load an example.
#include <iostream>
#include <vector>
using namespace std;
struct A {
union {
int i = 82, j;
};
int a = 98, b = 22;
};
int main() {
A p = {.a = 45};
cout << p.a << endl; // just here to prevent optimization
cout << p.i << endl; // same
return 0;
}
That can be tested here : Godbolt test case
And I get: warning: missing initializer for member 'A::' [-Wmissing-field-initializers]
Now, the real issue is that a real lack of initialization can have a lot of consequences (think real life damage).
Please note that all the fields have a default initialization. And the unit tests show that fields are properly initialized (luck?)
Again, I am VERY uncomfortable with this warning.
The need:
In production code, the struct is big enough for me not willing to manually initialize all the members with this notation (and with many initialization strategies it becomes unbearable/error prone very quickly)
How can I manage to make this warning disappear (again, think damage in case of a lack of initialization), without just turning off -Wextra
?
Update:
I saw other related questions, but given the risks I really want to have an insight on this particular case.
Update 2:
Reading the assembly for different compilers, they seem to take the proper course of action, but only GCC complains.