Eigen::Ref<> as a member variable

不想你离开。 提交于 2019-12-09 01:42:33

问题


I need a class to have an Eigen::Ref variable as a static member which would be initialized through an init static method. Something like this:

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;
}

This doesn't compile. I get an error which looks like this:

/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); \

Generally speaking, it looks like an Eigen::Ref cannot be assigned to another Eigen::Ref. Does anyone know why this restriction is in place and if there is a way to store a Ref as a static member variable of a class?

PS: I'm using Eigen::Ref because the documentation here: https://eigen.tuxfamily.org/dox-devel/classEigen_1_1Ref.html makes it sound like it is the right pick as the generic type to use when implementing functions which should work on most Eigen types (for instance, in my case, on VectorXd and Map).


回答1:


In your case you should very likely better use a VectorXd, otherwise you would have to make sure that the VectorXd you passed to init is never destroyed.

The only reason to use a Ref here would be to allow initializing data with, e.g., a column of a Matrix without any copy.

Finally, if you want to reassign a Ref to reference another buffer, then use a placement new to re-call the constructor of Ref. Don't forget to call the destructor first.



来源:https://stackoverflow.com/questions/40296924/eigenref-as-a-member-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!