TemplateKeyword_flexible.cpp 677 B

12345678910111213141516171819202122
  1. #include <Eigen/Dense>
  2. #include <iostream>
  3. using namespace Eigen;
  4. template <typename Derived1, typename Derived2>
  5. void copyUpperTriangularPart(MatrixBase<Derived1>& dst, const MatrixBase<Derived2>& src)
  6. {
  7. /* Note the 'template' keywords in the following line! */
  8. dst.template triangularView<Upper>() = src.template triangularView<Upper>();
  9. }
  10. int main()
  11. {
  12. MatrixXi m1 = MatrixXi::Ones(5,5);
  13. MatrixXi m2 = MatrixXi::Random(4,4);
  14. std::cout << "m2 before copy:" << std::endl;
  15. std::cout << m2 << std::endl << std::endl;
  16. copyUpperTriangularPart(m2, m1.topLeftCorner(4,4));
  17. std::cout << "m2 after copy:" << std::endl;
  18. std::cout << m2 << std::endl << std::endl;
  19. }