Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I can't understand how to write templated function that accepts both vector and matrix column?

For example:

template<typename T>
void foo(
    const Eigen::MatrixX<T>& M){

}

int main(){
  Eigen::VectorX<double> v(3);
  Eigen::MatrixX<double> m(4,3);

  foo(m); // fine
  foo(m.col(0)); // broken
  foo(m.row(0)); // broken
  foo(v); // broken
}

only foo(m); is ok.

I've seen examples that do this with predefined types and I've seen examples that explore templates. Neither of them do shows how to solve described task with templated function.

Edit: Also I would like to pass dynamic size vector and, but not necessary, fixed size


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
513 views
Welcome To Ask or Share your Answers For Others

1 Answer

I could get this to work using MatrixBase:

#include <Eigen/Dense>

template<typename T>
void foo(const Eigen::MatrixBase<T>& M){}

int main(){
    Eigen::Vector3d v(3);
    Eigen::Matrix<double,4,3> m(4,3);
    Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> q(5,6);

    foo(m);
    foo(m.col(0));
    foo(m.row(0));
    foo(v);
    foo(q);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...