<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 - Incorrect initialization of static data members in recursive class templates"
   href="https://bugs.llvm.org/show_bug.cgi?id=49458">49458</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect initialization of static data members in recursive class templates
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>maltsevm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When the following code is compiled with Clang and run, it produces an
assertion failure:

$ cat test.cpp
#include <assert.h>

template <int x, int y>
struct GCD {
  static int value;
};

template <int x, int y>
int GCD<x, y>::value = GCD<y, x % y>::value;

template<int x>
struct GCD<x, 0> {
  static int value;
};

template<int x>
int GCD<x, 0>::value = x;

int main() {
  assert((GCD<9, 6>::value) == 3);
}

$ clang test.cpp
$ ./a.out
a.out: test.cpp:20: int main(): Assertion `(GCD<9, 6>::value) == 3' failed.
Aborted (core dumped)

>From the generated LLVM IR it is clear that GCD<3, 0>::value is initialized in
the data section, whereas GCD<9, 6>::value and GCD<6, 3>::value initially have
value zero and are initialized at run time. The order of initialization however
is incorrect (according to my understanding of
<a href="http://eel.is/c++draft/temp.point#4">http://eel.is/c++draft/temp.point#4</a>), as if was performed using the following
code:

GCD<9, 6>::value = GCD<6, 3>::value;
GCD<6, 3>::value = GCD<3, 0>::value;

The program works as expected when compiled with GCC.</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>