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.
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?`
const Eigen::Ref<const Eigen::MatrixXd>&
const Eigen::Ref<const Eigen::MatrixXd>
const Eigen::Ref<Eigen::MatrixXd>&
const Eigen::Ref<Eigen::MatrixXd>
Eigen::Ref<const Eigen::MatrixXd>&
Eigen::Ref<const Eigen::MatrixXd>
Eigen::Ref<Eigen::MatrixXd>&
Eigen::Ref<Eigen::MatrixXd>
For completeness I listed every possible combination of const usage and the reference.
See Question&Answers more detail:os