lundi 26 juillet 2021

Can decltype make less recompilation

I'm working on a cpp project, let's say there is such a piece of code:

// header1.h
struct Test {
    int a;
    bool func(int b) { return a < b; }
};

// header2.h
#include "header1.h"
void myfunc(int aa) {
    Test t;
    t.a = aa;
    bool res = t.func(3);
    // something else
}

// header3.h
#include "header1.h"
void myfunc(decltype(Test::a) aa) {   // CHANGED HERE
    Test t;
    t.a = aa;
    bool res = t.func(3);
    // something else
}

I was always coding like headedr2.h. But today, I got such a case, where the type of Test::a in the header1.h may change into uint8_t, int32_t etc in the future. If it changes, the header2.h should be changed too. (I don't want to make any implicit-conversion.)

That means, if header1.h changes, I have to change header2.h, as a result, all of files including header2.h have to be recompiled.

Now, I'm thinking if it is possible to use decltype just like header3.h to avoid recompilation. In other words, I'm asking if I code header3.h instead of header2.h, is it possible to avoid recompilation of the files which included header3.h after changing the type of Test::a in header1.h?

Aucun commentaire:

Enregistrer un commentaire