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