Eigen :: Ref<作为成员变量 [英] Eigen::Ref<> as a member variable

查看:175
本文介绍了Eigen :: Ref<作为成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个具有Eigen :: Ref变量作为静态成员的类,该变量将通过 init 静态方法进行初始化。像这样的东西:

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

通常来说,它看起来像一个Eigen :: Ref无法分配给另一个Eigen ::参考
有人知道为什么存在此限制,以及是否有办法将Ref存储为类的静态成员变量吗?

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正在使用Eigen :: Ref,因为此处的文档: https://eigen.tuxfamily.org /dox-devel/classEigen_1_1Ref.html 听起来像是在实现应适用于大多数本征类型(例如,在我的情况下,在VectorXd和Map上)的函数时使用的通用类型的正确选择。

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

推荐答案

在您的情况下,您最好使用 VectorXd ,否则必须确保传递给init的 VectorXd 永远不会被破坏。

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.

唯一原因使用 Ref 此处将允许使用例如数据 > Matrix 没有任何副本。

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.

最后,如果要重新分配 Ref 引用另一个缓冲区,然后使用new放置重新调用Ref的构造函数。不要忘记先调用析构函数。

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.

这篇关于Eigen :: Ref&lt;作为成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆