https://bugs.llvm.org/show_bug.cgi?id=49458

            Bug ID: 49458
           Summary: Incorrect initialization of static data members in
                    recursive class templates
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected],
                    [email protected]

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
http://eel.is/c++draft/temp.point#4), 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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to