【问题标题】:Clang/LLVM 9 and 10 SIGSEGV for inline static class members. Bug?用于内联静态类成员的 Clang/LLVM 9 和 10 SIGSEGV。漏洞?
【发布时间】:2020-06-30 08:30:06
【问题描述】:

当成员是另一个类/结构时,在 Clang 中使用 inline static 类成员会给我带来意想不到的行为: 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;
}

预期输出,适用于 Clang 8.0.0、gcc、msvc:

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

Clang 9.0.0 及更高版本的实际输出:139 (SIGSEGV)

这是一个错误,还是我错过了什么?

【问题讨论】:

    标签: static c++17 inline llvm-clang


    【解决方案1】:

    对我来说,这听起来像是一个非常简单的错误:

    [class.static.data]

    一个 内联静态数据成员可以在类定义中定义,并且可以指定 brace-or-equal-initializer

    你的代码符合这个:

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

    【讨论】:

    • 我会提交一份错误报告。但对我来说似乎很疯狂,这种“简单”的东西没有被回归。
    猜你喜欢
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多