0

I am using the Eigen template library for matrices. I am puzzled by compiler error in the Foo2 class while Foo1 (not templated) with almost the same code passes just fine:

#include<Eigen/Core>

struct Foo1{
    static Eigen::Matrix<double,3,1> bar(const Eigen::Matrix<double,6,1>& v6){
        return v6.head<3>();
    }
};

template<typename Scalar>
struct Foo2{
    static Eigen::Matrix<Scalar,3,1> bar(const Eigen::Matrix<Scalar,6,1>& v6){
         return v6.head<3>();
    }
};

gives (clang 3.4, gcc 4.8):

a.cc:8:53: error: expected expression
        static Vec3 bar(const Vec6& v6){ return v6.head<3>(); }
                                                           ^
1 error generated.

(does it mean the compiler parses the < as "less than " instead of start of template arguments?).

Any clue as to what's going on?

eudoxos
  • 18,545
  • 10
  • 61
  • 110

1 Answers1

4

Yes, that's exactly what it means. You need to tell the compiler that head is a template and not a data member:

return v6.template head<3>();

The reason that the compiler can't tell is because it doesn't know what the instantiated type of v6 is (since it depends on the template parameter Scalar).

We had a question with the same answer earlier today. For a more in-depth explanation, see Where and why do I have to put the “template” and “typename” keywords?.

Community
  • 1
  • 1
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324