0

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?

David Doria
  • 9,873
  • 17
  • 85
  • 147

1 Answers1

2

You have to use template keyword:

template <typename TVector>
Plane(const TVector& coefficients) {
    coefficients.template cast<double>();
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Yep, that does it. Can you say what this is called so I can look up when I need to use it? – David Doria Feb 11 '16 at 20:50
  • @DavidDoria He did. It is the [`template` keyword](http://en.cppreference.com/w/cpp/keyword/template) – clcto Feb 11 '16 at 20:51
  • @DavidDoria see the link above your original question, which has been marked as duplicate. The link explains this phenomenon in full. – iksemyonov Feb 11 '16 at 21:08