Non-Designated Initializers (Compiler Flag Warning)

73 Views Asked by At

Let us assume we have the following code:

struct MyStruct
{
    double height, width;
};

int main()
{
    const MyStruct a { 5.1, 2.3 };
    const MyStruct b { .height = 3.5, .width = 1.8 };

    return 0;
}

Since C++20 we have Designated Initializers, which means we can use syntax like { .height = 3.5, .width = 1.8 }. Now, if the internal order of fields height and width changes, then { 5.1, 2.3 } still works (even though it's incorrect now). Whereas { .height = 3.5, .width = 1.8 } fails to compile, which requires us to correct the initialization order.

So I was wondering, if there were any compiler flags (in g++ or msvc) to warn about the usage of non-designated initializers like { 5.1, 2.3 }? This could help identify cases, where the internal order was switched without updating (or incorrectly updating) usages elsewhere in a library.

0

There are 0 best solutions below