C ++静态数据成员初始化奇数

时间:2020-10-31 06:44:11

标签: c++ initialization

我使用-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。有押韵或理由吗?

0 个答案:

没有答案