Search code examples
c++templateseigeneigen3

Eigen template on scalar type fails to compile fixed-size submatrix operation


Can someone explain why this MWE fails to compile:

template<typename Scalar>
class ScalarTemplate {
public:
    static Eigen::Matrix<Scalar, 2, 1> tail(){
        Eigen::Matrix<Scalar, 3, 1> m;
        return m.tail<2>();
    }
};

int main() {
    auto tail = ScalarTemplate<float>::tail();
}

Pretty straightforward, all relevant Eigen templates should be initialized with float and all container sizes should be known at compile time. Yet this produces the following:

../src/main.cpp: In static member function 'static Eigen::Matrix<_Scalar, 2, 1> ScalarTemplate<Scalar>::tail()':
../src/main.cpp:24:26: error: expected primary-expression before ')' token
         return m.tail<2>();
                          ^
../src/main.cpp: In instantiation of 'static Eigen::Matrix<_Scalar, 2, 1> ScalarTemplate<Scalar>::tail() [with Scalar = float]':
../src/main.cpp:29:40:   required from here
../src/main.cpp:24:22: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'
         return m.tail<2>();

Same thing happens with all submatrix operations as far as I could tell. Eigen is version 3.3.7.


Solution

  • The error message was a bit confusing so it took me some time to figure out the fact that the submatrix operations are themselves template calls inside a template class, so the correct call is:

    m.template tail<2>();