繁体   English   中英

C++ static 特征值中的数据成员初始化错误

[英]C++ static data member initialization error in Eigen

我在 Ubuntu 18.04 上使用 gcc-10 和 Eigen 3.3.9。 此代码段编译并运行良好:

struct S {
  static inline Eigen::Vector3f u{1, 1, 1};
};

int main() {
  Eigen::Vector3f u(1, 1, 1);
}

但后一个版本的初始化应用于 static 数据成员时,编译失败:

struct S {
  static inline Eigen::Vector3f u(1, 1, 1);
};

带有此错误消息: error: expected identifier before numeric constant

这是为什么?

正如添加该特性的论文中所讨论的,希望默认成员初始化器支持正向查找以与(构造函数的)成员初始化器保持一致。 然而,成员 function 参数列表并不是这样一个完整的类上下文。 这导致 function 和变量声明之间的歧义:

int I() {return -7;}
struct A {
  int x(I());
  typedef int I;
};

If A::x is a variable, then lookup for I finds the type A::I , which means that the declaration of x can be interpreted as a function declaration (accepting a function pointer), so it's a function. 如果它是 function,则查找I不考虑(后续) A::I ,因此I::I并且A::x声明了一个初始化为 -7 的变量。 交换::IA::I给出了两种自洽的解释,而不是没有。

有一条规则是 class 不得更改在其定义中执行的任何名称查找的含义,但尚不清楚它是否适用于此处,因为在变量解释下,查找不会改变。 相反,句法可能性被完全排除在外,尽管当然存在完全明确的情况。

暂无
暂无

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

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