正确使用Eigen :: Ref<>班级 [英] Correct usage of the Eigen::Ref<> class

查看:179
本文介绍了正确使用Eigen :: Ref<>班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eigen引入了Ref<>类,以在不需要编写模板函数时以Eigen对象作为参数来编写函数,而无需使用不必要的临时变量.可以在此处中阅读.

Eigen has introduced the Ref<> class to write functions with Eigen objects as parameters without the use unnecessary temporaries, when writing template functions is not wanted. One can read about this here.

当进一步搜索Internet时,我发现了使用Ref<>类的几个不同的参数声明.在Eigen文档中,在第一个示例中,它们将const Eigen::Ref<const Eigen::MatrixXf>&用作只读参数.在第二个示例中,为读写参数引入了Eigen::Ref<Eigen::MatrixXd>,但此处const Eigen::Ref<const Eigen::MatrixXd>用于只读参数(无参考).所以我的问题是:

When searching the internet further, I found several different declarations of parameters using the Ref<> class. In the Eigen documentation they use const Eigen::Ref<const Eigen::MatrixXf>& for a read-only parameter in the first example. In the second example Eigen::Ref<Eigen::MatrixXd> is introduced for read-and-write parameters, BUT here const Eigen::Ref<const Eigen::MatrixXd> is used for read-only parameters (no reference). So my question is:

以下声明之间的区别是什么?什么时候使用?

What is the difference between the following declarations and when do I use which?`

  1. const Eigen::Ref<const Eigen::MatrixXd>&
  2. const Eigen::Ref<const Eigen::MatrixXd>
  3. const Eigen::Ref<Eigen::MatrixXd>&
  4. const Eigen::Ref<Eigen::MatrixXd>
  5. Eigen::Ref<const Eigen::MatrixXd>&
  6. Eigen::Ref<const Eigen::MatrixXd>
  7. Eigen::Ref<Eigen::MatrixXd>&
  8. Eigen::Ref<Eigen::MatrixXd>
  1. const Eigen::Ref<const Eigen::MatrixXd>&
  2. const Eigen::Ref<const Eigen::MatrixXd>
  3. const Eigen::Ref<Eigen::MatrixXd>&
  4. const Eigen::Ref<Eigen::MatrixXd>
  5. Eigen::Ref<const Eigen::MatrixXd>&
  6. Eigen::Ref<const Eigen::MatrixXd>
  7. Eigen::Ref<Eigen::MatrixXd>&
  8. Eigen::Ref<Eigen::MatrixXd>

为完整起见,我列出了const用法和参考的所有可能组合.

For completeness I listed every possible combination of const usage and the reference.

推荐答案

通常,使用像Ref<T>&这样的非const引用绝不是一个好主意,因为这仅在调用方已经具有Ref<T>对象的情况下才有效手.这将丢弃5和7.

In general, using a non const reference like Ref<T>& is never a good idea because this will only work if the caller already has a Ref<T> object at hand. This discards 5 and 7.

情况3和4毫无意义,它们通常不会编译.

The cases 3 and 4 does not make sense, and they won't compile in general.

然后,const Ref<const T>const Ref<const T>&之间没有太大区别,因为不太可能使用现有的Ref<const T>对象调用该函数,因此大多数情况下都必须创建Ref<const T>无论如何.尽管如此,我们仍然建议2或6选1.

Then, there is not much difference between a const Ref<const T> and a const Ref<const T>& because it is unlikely that the function is called with an existing Ref<const T> object, and so a Ref<const T> will have to be created in most cases anyway. Nevertheless, we could still recommend 1 over 2 or 6.

因此,总而言之,我们建议将Ref<T>用作可写引用,并建议const Ref<const T>&用作const引用.

So in summary, we could recommend Ref<T> for a writable reference, and const Ref<const T>& for a const reference.

如果要通过使用

The option 6, Ref<const T>, might still be interesting if you want to change the matrix that is referenced (not its content) through a call to Ref constructor using placement new.

这篇关于正确使用Eigen :: Ref&lt;&gt;班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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