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
?
No, if you change header1.h
you have to recompile everything that includes header1.h
.
The reason is: how could the compiler generate the code for myfunc
without knowing the type of its argument, or without knowing the Test
type that you use inside myfunc
?
If you change the struct Test
, altering the type of its members, all the code generated using an outdated declaration of Test
is likely to fail to compile at the linking stage, or it could have an unpredictable behavior at runtime.