首页 > 解决方案 > C++ 静态数据成员初始化异常

问题描述

-std=gnu++17我使用和-std=gnu++2a选项使用 gcc-10.1 尝试了这个。如果未注释 A 行,则此代码段将无法编译:

struct Vec3f {
  float x, y, z;
  Vec3f(float x, float y, float z) : x(x), y(y), z(z) {};
};

struct S {
  static inline Vec3f a{1, 1, 1};
  static inline auto b = Vec3f(1, 1, 1);
  static inline Vec3f c(1, 1, 1);  // Line A
};

int main(int, char**) {
  Vec3f c(1, 1, 1);
  return c.z;
}

产生此错误:

[build] ../main.cpp:16:25: error: expected identifier before numeric constant
[build]    static inline Vec3f c(1, 1, 1);
[build]                          ^
[build] ../main.cpp:16:25: error: expected ‘,’ or ‘...’ before numeric constant

尽管在函数Vec3f c(1, 1, 1);内部被愉快地使用main。有什么押韵或理由吗?

标签: c++initialization

解决方案


推荐阅读