Search code examples
c++templateseigeneigen3

Strange error in templated constructor


I am getting a strange compiler error when trying to cast an argument in a templated constructor. Here is a minimal example:

#include <Eigen/Dense>

class Plane
{
public:
    template <typename TVector>
    Plane(const TVector& coefficients) {
        coefficients.cast<double>(); // compiler error on this line
    }

// This compiles fine
//    Plane(const Eigen::Vector3d& coefficients) {
//        coefficients.cast<double>();
//    }
};

int main(){return 0;}

The error is:

expected primary-expression before 'double'
expected ';' before 'double'

Since this class is never instantiated (main() is empty), I thought that the compiler wouldn't even see the function template at all, so I'm very confused as to how there is an error in this expression?


Solution

  • You have to use template keyword:

    template <typename TVector>
    Plane(const TVector& coefficients) {
        coefficients.template cast<double>();
    }