类内静态成员初始化

yur*_*hek 5 c++ static-members constant-expression in-class-initialization c++11

特定

struct X {};

constexpr auto x = X{};

struct S {
    static constexpr auto& rx = x;  
};
Run Code Online (Sandbox Code Playgroud)

gcc 4.8说

错误:非常量的类内初始化对静态成员'S :: rx'无效

static constexpr auto& rx = x;  
                            ^
Run Code Online (Sandbox Code Playgroud)

错误:(需要进行类外初始化)

错误:'S :: rx'在声明时无法通过非常量表达式初始化

我希望x它是一个常量表达式,适合这种初始化.这是一个gcc bug吗?如果没有,这里发生了什么?

Nip*_*dar 0

您可以执行以下操作:

struct X {};

const auto x = X{};

struct S {
    static constexpr auto& rx = x;  
};
Run Code Online (Sandbox Code Playgroud)