C++ C++;静态数据成员初始化异常
我在gcc-10.1中使用了C++ C++;静态数据成员初始化异常,c++,initialization,C++,Initialization,我在gcc-10.1中使用了-std=gnu++17和-std=gnu++2a选项进行了尝试。如果未注释行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); st
-std=gnu++17
和-std=gnu++2a
选项进行了尝试。如果未注释行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
尽管
vec3fc(1,1,1)代码>在main
函数中被愉快地使用。这有什么道理吗?因为初始化静态类成员不是合法的方法。@JHBonarius为什么不呢?埋在C++的法文里吗?这种不规则现象有什么好的原因吗?聚合初始化在这里是个奇怪的问题。通常情况下,任务总是需要的。似乎对我很有用。。。但后来我发现了一点未注释的行。请帮我一个忙,把非工作版本发出去。@user4581301完成。很抱歉给你带来了困惑。