mardi 21 novembre 2023

How to make initializer lists robust to code changes?

There is this code:

#include <iostream>

struct Range {
    //bool enabled = false;
    //int index = 0;
    int begin = 0;
    int end = 0;
};

int main()
{
    Range r = { 2, 3 };
    std::cout << r.begin << " " << r.end;
}

If I change the contents of the Range structure by uncommenting one of the commented lines, the program compiles, but does not work correctly. If I add a bool type field to the beginning of Range, the compiler at least issues a warning. But if I add a field of type int to the beginning of Range, then there is not even be any warnings.

Is there any way to make sure that when the contents of a structure changes, all initializer lists that initialize it become invalid or at least issue a warning during compilation? Or maybe is there some other reliable way to find all initializer lists of this structure in the code?

In general, I need some reliable way to find all initializers and make sure that the structure is correctly initialized after changes.

Aucun commentaire:

Enregistrer un commentaire