Templated Eigen SparseMatrix with triangularView

Alec Jacobson

September 14, 2013

weblog/

I was trying to use Eigen's triangularView for a templated SparseMatrix like this:

template <typename T> void foo()
{
  Eigen::SparseMatrix<T> A;
  A.triangularView<Upper>();
}

But I kept getting errors like:

 In function 'void foo()':
22:34: error: expected primary-expression before ')' token
 In instantiation of 'void foo() [with T = double]':
81:15:   required from here
22:3: error: no match for 'operator<' in 'A.Eigen::SparseMatrixBase<Derived>::triangularView<Mode> < (Eigen::._64)2u'

Turns out this can be fixed by appending the word template before triangularView:

template <typename T> void foo()
{
  Eigen::SparseMatrix<T> A;
  A.template triangularView<Eigen::Upper>();
}