我需要一个类有一个 Eigen::Ref 变量作为静态成员,该变量将通过init静态方法。像这样的东西:
class CostFunction {
public:
static Eigen::Ref<Eigen::VectorXd> data;
static void init(const Eigen::Ref<Eigen::VectorXd>& d) {
data = d;
}
CostFunction() {}
};
int main() {
Eigen::VectorXd data = Eigen::VectorXd::Random(30);
CostFunction cf;
cf.init(data);
return 0;
}
这不能编译。我收到一个错误,如下所示:
/var/tmp/doNotRemove/builds/fit3dceres/RHEL6_AMD64_GCC484_OPT/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘Eigen::RefBase<Derived>& Eigen::RefBase<Derived>::operator=(const Eigen::RefBase<Derived>&) [with Derived = Eigen::Ref<const Eigen::Matrix<double, -1, 1> >]’:
/var/tmp/doNotRemove/builds/fit3dceres/RHEL6_AMD64_GCC484_OPT/include/eigen3/Eigen/src/Core/Ref.h:229:77: required from here
/var/tmp/doNotRemove/builds/fit3dceres/RHEL6_AMD64_GCC484_OPT/include/eigen3/Eigen/src/Core/util/Macros.h:608:26: error: use of deleted function ‘Eigen::MapBase<Eigen::Ref<const Eigen::Matrix<double, -1, 1> >, 0>& Eigen::MapBase<Eigen::Ref<const Eigen::Matrix<double, -1, 1> >, 0>::operator=(const Eigen::MapBase<Eigen::Ref<const Eigen::Matrix<double, -1, 1> >, 0>&)’
Base::operator=(other); \
一般来说,看起来一个 Eigen::Ref 不能分配给另一个 Eigen::Ref。
有谁知道为什么存在此限制以及是否有办法将 Ref 存储为类的静态成员变量?
PS:我使用 Eigen::Ref 因为这里的文档:https://eigen.tuxfamily.org/dox-devel/classEigen_1_1Ref.html听起来它是在实现适用于大多数特征类型的函数时使用的泛型类型的正确选择(例如,在我的例子中,在 VectorXd 和 Map 上)。