Correct usage of the Eigen::Ref<> class

后端 未结 1 1163
温柔的废话
温柔的废话 2020-12-05 00:35

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 w

1条回答
  •  离开以前
    2020-12-05 01:09

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

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

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

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

    The option 6, Ref, 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.

    0 讨论(0)
提交回复
热议问题