Search code examples
staticc++17inlinellvm-clang

Clang/LLVM 9 and 10 SIGSEGV for inline static class members. Bug?


Using inline static class members in Clang gives me unexpected behaviour when the member is another class/struct: https://godbolt.org/z/mbH6k7

// std=c++17
#include <iostream>

struct A {
    double a = 42;
    A() { std::cout << "A()" << std::endl; }
};

inline static A a{}; // No problem

namespace N {
    inline static A a{}; // No problem
}

struct B {
    B() { std::cout << "B()" << std::endl; }
    inline static double d; // No problem with built-in types
    A& a1 = N::a; // No problem
    inline static A a2 = N::a; // No problem
    inline static A a3{}; // <-- Problem here!
};

B b1;
inline static B b2;

int main() {
    return 0;
}

Expected output, works in Clang 8.0.0, gcc, msvc:

A()
A()
A()
B()
B()

Actual output for Clang 9.0.0 and onwards: 139 (SIGSEGV).

Is this a bug, or what am I missing?


Solution

  • This sounds like a pretty cut-and-dry bug to me:

    Per [class.static.data]

    An inline static data member may be defined in the class definition and may specify a brace-or-equal-initializer.

    Your code conforms to this:

    struct B {
        // ...
        inline static A a3{};
    };