title: How to deal with Boost Spirit X3 causing a "static initialization order fiasco" in Visual Studio 2019?
body: <p>I'm working on a nontrivial parser in C++ on top of boost::spirit::x3. I am splitting up my parsing code into logical units some of which are dependencies of each other. For example, one unit is an expression parser that also exposes an indentifier parser. Many higher level syntactic constructs of the target language include expressions and identifiers so this unit is frequently a dependency. I have split the code into triples of files <a href="https://www.boost.org/doc/libs/develop/libs/spirit/doc/x3/html/spirit_x3/tutorials/minimal.html" rel="nofollow noreferrer">as the documentation recommends</a>. If there are units <em>foo</em>, <em>bar</em>, and <em>quux</em>, I have files like:</p>
  
  <pre><code>parser
      foo.h
      foo_def.h
      foo.cpp
      bar.h
      bar_def.h
      bar.cpp
      quux.h
      quux_def.h
      quux.cpp
  </code></pre>
  
  <p>where the <code>.h</code> expands <code>BOOST_SPIRIT_DECLARE</code>; the <code>_def.h</code> expands <code>BOOST_SPIRIT_DEFINE</code>, and the <code>.cpp</code> expands <code>BOOST_SPIRIT_INSTANTIATE</code>. I have run into a persistent problem in which my code errors out at startup due to static initialization fiasco in Spirit code (line 160 of .../x3/nonterminal/rule.hpp) but only when running a debug build.</p>
  
  <p>Based on stackoverflow questions and answers like <a href="https://stackoverflow.com/questions/41770375/strange-semantic-behaviour-of-boost-spirit-x3-after-splitting">this one</a> and <a href="https://stackoverflow.com/questions/43679200/x3-parser-segfaults-with-debug-output-boost-spirit-x3-debug">this one</a> plus the fact that the code executes without error when built in the release configuration, I believe there is nothing wrong with my code. I can see two workarounds if there is no way to fix my code in debug:</p>
  
  <ol>
  <li><p>Only in debug builds use a pragma declaration to change the order of static initialization. (Changing the order of the items in the .vcxproj file did not affect the initialization order) In Visual Studio if I put a <code>#pragma init_seg(lib)</code> in the .cpp file of my expression parser the problem goes away.</p></li>
  <li><p>Only in debug builds include superfluous <code>_def.h</code> files where necessary e.g. if <em>quux</em> depends on <em>bar</em> above then including <code>bar_def.h</code> in <code>quux.cpp</code> will fix the problem, but defeats the purpose of splitting the parser into multiple files.</p></li>
  </ol>
  
  <p>I am wondering generally what the state of this problem is? It seems to be a known issue but has been around a long time and so I should not expect it to be fixed at the Spirit level? Is there some better way to structure my code such that this problem does not occur? I was wondering if it would be possible to, for example, create all parsers and their child parsers as static variables in function scope created on first use but there is no examples like this in the literature so I am unsure if it is possible? </p>