2

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.

Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
  • I don't thing there is a warning for that... but should probably done "easily" with clang tooling. – Jarod42 Jun 14 '22 at 08:04

0 Answers0