2

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).

delacoder
  • 35
  • 4

1 Answers1

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.

ggael
  • 28,425
  • 2
  • 65
  • 71
  • 1
    Thanks for the answer ggael, placement new does the trick. In addition, I had to turn my static member variable into a pointer to allow its initialization (no Ref default constructor). In this case, not copying the VectorXd passed as parameter is precisely my goal (it is very large in the real application). If Ceres was using classic polymorphism, I would use a shared_ptr (or something like that) as the type of my variable, but their implementation makes this very difficult. If you know of a better way to do what I'm after, please feel free to share it. Thanks again. – delacoder Oct 28 '16 at 22:56
  • Except that occasionally I need to pass a Map (instead of a VectorXd) which cannot be cast into a VectorXd. – delacoder Oct 29 '16 at 17:19
  • @ggael can look over here please http://stackoverflow.com/questions/40434976/eigen-sparse-lu-solver-return-value – Michael Medvinsky Nov 05 '16 at 16:49