简体   繁体   English

类内静态成员初始化

[英]In-class static member initialization

Given 特定

struct X {};

constexpr auto x = X{};

struct S {
    static constexpr auto& rx = x;  
};

gcc 4.8 says gcc 4.8说

error: non-constant in-class initialization invalid for static member 'S::rx' 错误:非常量的类内初始化对静态成员'S :: rx'无效

static constexpr auto& rx = x;  
                            ^

error: (an out of class initialization is required) 错误:(需要进行类外初始化)

error: 'S::rx' cannot be initialized by a non-constant expression when being declared 错误:'S :: rx'在声明时无法通过非常量表达式初始化

I expect x to be a constant expression, suitable for such initialization. 我希望x是一个常量表达式,适合这种初始化。 Is this a gcc bug? 这是一个gcc bug吗? If not, what is going on here? 如果没有,这里发生了什么?

这是一个错误,似乎已经报道过了

You may do the below instead: 您可以改为:

struct X {};

const auto x = X{};

struct S {
    static constexpr auto& rx = x;  
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM