You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++17中类的constexpr数据成员是否默认隐含static属性?

结论

C++17标准中,constexpr修饰类数据成员时不会默认隐含static属性,你此前的调研结论完全正确:constexpr和static是相互独立的修饰符。

对提及评论的说明

你看到的评论描述的是constexpr static组合修饰类数据成员的特性,完全无法推导出constexpr自带static属性:

One more thing, in C17, constexpr static data member variables will be inline too. That means you can omit the out of line definition of static constexpr variables, but not static const.
这段内容仅说明:C
17起,同时被声明为static constexpr的类数据成员会默认具备inline属性,不需要再在类外单独写该静态成员的定义语句,仅此而已,和constexpr是否隐含static没有关联。

语法验证示例

我们可以通过两个C++17下的合法语法示例,明确二者的独立关系:

  • 非静态constexpr成员在C++17完全支持,类的每个实例会持有独立的该成员变量:
class Demo {
public:
    // 非静态constexpr数据成员,不存在隐含static的情况
    constexpr int num = 5;
};
  • 静态constexpr成员属于类本身共享的属性,static修饰符必须显式声明:
class Demo {
public:
    // 静态constexpr数据成员,static需要手动添加
    static constexpr int num = 5;
    // C++17起不需要额外在类外添加`const int Demo::num;`这类定义语句
};

内容的提问来源于stack exchange,提问作者Vasi Marin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 14:45:03