Consider the following header file, which consists of slow constexpr function, which is used to initialize a global variable:
constexpr int slow_func() {
for (int i = 0; i < 100*1024*100; ++i)
;
return 0;
}
constexpr int g_val = slow_func();
Calling this function takes ~10s
Now, if this header gets #included in multiple translation units, the compilation time increases by each translation unit that #includes this file
With hundreds of translations units, compilation now takes an unreasonable amount of time.
Since this is a constexpr function, I assumed that the compiler would evaluate the return value of this function only once, and use the same value in the different translation units
Is there a way to tell the compiler to evaluate the value of each 'g_val' only once? If not, what can be done?
I'm currently using g++-5.4, but I suppose the standard mandates this behaviour (Even though I didn't find it in the current standard)