title: VS2015 C++ static initialization crash, possible bug
body: <p>I'm seeing something weird happening with Visual Studio 2015 Community. Code that worked perfectly in VS2012 crashes at startup when ported to VS2015, before main is invoked: the classic symptoms of some static initialization mess. I have a few static variables, but used properly, with the standard "Construct On First Use" pattern, e.g.:</p>
  
  <pre><code>const int&amp; i()
  {
    static int *v = new int(1);
    return *v;
  }
  </code></pre>
  
  <p>The code above causes an assert from the runtime while initializing the program (see image <a href="http://i.stack.imgur.com/nsQGS.png" rel="nofollow">http://i.stack.imgur.com/nsQGS.png</a>). Pressing retry simply quits the program: no call stack, no information whatsoever.</p>
  
  <p>The code below however works flawlessly:</p>
  
  <pre><code>const int&amp; i()
  {
    static int *v = nullptr;
    if (v == nullptr)
      v = new int(1);
    return *v;
  }
  </code></pre>
  
  <p>It seems to me that VS2015 is doing what looks like some (illegal) optimization (even in a debug build), by executing the static variable initialization code during program initialization, and not the first time that the function is called, as the C++ standard requires.
  I tried several variations of the code above: class methods, free functions, different objects (std::vector, cv::Mat), always with the same result: the static pointer has to be initialized to null or the program doesn't start.</p>
  
  <p>So... am I missing something or is VS2015 actually messing up badly?</p>
  
  <p>UPDATE: </p>
  
  <p>I spent some time trying to find the minimal setup that shows the problem, and this is what I got: </p>
  
  <ol>
  <li>create a project using the "CLR empty project" template.</li>
  <li>add a cpp file, containing just the offending function and main(). Main can be empty, it doesn't matter: the bug occurs BEFORE it is even reached.</li>
  <li>add 'main' as entry point (or you get a link error).</li>
  </ol>
  
  <p>The x86 version works, but the x64 doesn't. </p>
  
  <p>For comparison, a project with the identical code but created as "Win32 console application" doesn't have the problem, even after adding the /CLR option. I can see differences in the vcxproj files, but none justifies the error, although one or more of them clearly IS the cause.</p>
  
  <p>Is there a way to upload a zip with the project?</p>