<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Aggregates containing elements with constexpr constructors are not statically initalized"
   href="https://bugs.llvm.org/show_bug.cgi?id=45367">45367</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Aggregates containing elements with constexpr constructors are not statically initalized
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++2a
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>billy.oneal@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=23295" name="attach_23295" title="repro.cpp">attachment 23295</a> <a href="attachment.cgi?id=23295&action=edit" title="repro.cpp">[details]</a></span>
repro.cpp

Previously reported as <a href="https://github.com/microsoft/STL/issues/661">https://github.com/microsoft/STL/issues/661</a>

In the following example, should_be_statically_initalized should be eligible
for static initialization, then the dynamic initializer for foo that calls
bar() should run, resulting in the asserts in main(). However, clang only seems
to give these static initializers if the varaible is {}'d.

C:\Users\billy\Desktop>type repro.cpp
#include <assert.h>

struct FakeAtomic {
    int example;

    constexpr FakeAtomic() noexcept : example{} {}
    int operator++() { return ++example; }
    int operator++(int) { return example++; }
    int load() const { return example; }
};

struct Meow {
    FakeAtomic data[2];
    char padding[64];
};

int bar();

int foo = bar();
#ifdef WORKAROUND
Meow should_be_statically_initalized[256]{};
#else
Meow should_be_statically_initalized[256];
#endif

int bar() { return should_be_statically_initalized[0].data[0]++; }

int main() {
    assert(foo == 0);
    assert(should_be_statically_initalized[0].data[0].load() == 1);
}

C:\Users\billy\Desktop>clang-cl /EHsc /W4 /WX .\repro.cpp

C:\Users\billy\Desktop>.\repro.exe
Assertion failed: should_be_statically_initalized[0].data[0].load() == 1, file
.\repro.cpp, line 30

C:\Users\billy\Desktop>clang-cl /DWORKAROUND /EHsc /W4 /WX .\repro.cpp

C:\Users\billy\Desktop>.\repro.exe

C:\Users\billy\Desktop></pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>