1#include <Eigen/Core> 2#include <iostream> 3using namespace Eigen; 4using namespace std; 5 6template<typename Derived> 7Eigen::Block<Derived> 8topLeftCorner(MatrixBase<Derived>& m, int rows, int cols) 9{ 10 return Eigen::Block<Derived>(m.derived(), 0, 0, rows, cols); 11} 12 13template<typename Derived> 14const Eigen::Block<const Derived> 15topLeftCorner(const MatrixBase<Derived>& m, int rows, int cols) 16{ 17 return Eigen::Block<const Derived>(m.derived(), 0, 0, rows, cols); 18} 19 20int main(int, char**) 21{ 22 Matrix4d m = Matrix4d::Identity(); 23 cout << topLeftCorner(4*m, 2, 3) << endl; // calls the const version 24 topLeftCorner(m, 2, 3) *= 5; // calls the non-const version 25 cout << "Now the matrix m is:" << endl << m << endl; 26 return 0; 27} 28