So I'm building a syntax compiler with ANTLR and some of the generated code looks like this:
const int ExampleClass::EXAMPLEVAR = OtherExample::OTHEREXAMPLEVAR;
As you can see this fits the "static initialization order fiasco" description.
The problem is that one of the goals of this project is that the generated C++ code can be used as a base for further syntax compilation as easily as possible.
That's why the "construct on first use" paradigm might be a problem in this case: It would be much harder to differentiate between a static variable or a static function.
Now I have read on several occasions that the problem doesn't exist if these static variables are initialized in a single compilation unit.
So I have this idea of moving all these conflicting situations in a separate .cpp file ordered by their dependencies.
The generated code for these conflicting situations would look like this:
//StaticInitializations.cpp
#include "ExampleClass.h"
#include "OtherExample.h"
const int OtherExample::OTHEREXAMPLEVAR = 3;
const int ExampleClass::CHANNEL_TYPE_TV = OtherExample::OTHEREXAMPLEVAR;
My question is: would this work?