正确使用 Eigen::Ref>班级

2023-09-27C/C++开发问题
5

本文介绍了正确使用 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& 作为第一个示例中的只读参数.在第二个例子中,Eigen::Ref 被引入用于读写参数,但这里 const Eigen::Refcode> 用于只读参数(无参考).所以我的问题是:

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&
  2. const Eigen::Ref
  3. const Eigen::Ref&
  4. const Eigen::Ref
  5. Eigen::Ref&
  6. Eigen::Ref
  7. Eigen::Ref&
  8. Eigen::Ref

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

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

推荐答案

一般来说,使用像 Ref<T>& 这样的非常量引用从来都不是一个好主意,因为这只在以下情况下有效调用者手头已经有一个 Ref 对象.这会丢弃 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 Refconst Ref& 之间没有太大区别,因为函数不太可能是使用现有的 Ref 对象调用,因此无论如何在大多数情况下都必须创建 Ref.尽管如此,我们仍然可以推荐 1 比 2 或 6.

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 作为可写引用,const Ref& 作为常量引用.

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

选项 6,Ref,如果您想通过调用 Ref 构造函数使用 placement new.

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&gt;班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6