[llvm-bugs] [Bug 49458] New: Incorrect initialization of static data members in recursive class templates

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Mar 5 12:11:56 PST 2021


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: unassignedclangbugs at nondot.org
          Reporter: maltsevm at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210305/3b856d09/attachment.html>


More information about the llvm-bugs mailing list