简体   繁体   中英

Eigen templated function that accepts both Matrix col and vector

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM