Here is a minimal simplification that reproduces the error I'm seeing in a larger codebase. In a nutshell, I want to model line segments using different numeric types (int, double, etc.)
template<typename T>
class Vector2
{
public:
  Vector2(T x, T y)
  : x(x), y(y)
  {}
  template<typename U>
  Vector2<U> cast() const { return Vector2<U>((U)x, (U)y); }
  T x;
  T y;
};
template<typename T>
class Line2
{
public:
  Line2(Vector2<T> a, Vector2<T> b)
  : a(a), b(b)
  {}
  double gradient() const
  {
    Vector2<double> ad(a.cast<double>()); // ERROR HERE
    Vector2<double> bd(b.cast<double>()); // ERROR HERE
    return (bd.y - ad.y) / (bd.x - ad.x);
  }
  Vector2<T> a;
  Vector2<T> b;
};
Some operations upon line segments require conversion of the coordinates to double, such as a gradient calculation. Hence the vector type supports casting.
The call to cast works fine when called like this:
Line2<int> i(Vector2<int>(0,0), Vector2<int>(1,3));
Line2<double> d(Vector2<double>(0,0), Vector2<double>(0.5,1.23));
The following code calls cast indirectly via the gradient member function:
Line2<int> i(Vector2<int>(0,0), Vector2<int>(1,3));
Line2<double> d(Vector2<double>(0,0), Vector2<double>(0.5,1.23));
std::cout << "Line2<int>.gradient = " << i.gradient() << std::endl;
std::cout << "Line2<double>.gradient = " << d.gradient() << std::endl;
This gives the following compilation error:
test.cpp: In member function ‘double Line2<T>::gradient() const’:
test.cpp:28:31: error: expected primary-expression before ‘double’
     Vector2<double> ad(a.cast<double>());
                               ^
test.cpp:28:31: error: expected ‘)’ before ‘double’
test.cpp:29:31: error: expected primary-expression before ‘double’
     Vector2<double> bd(b.cast<double>());
                               ^
Why does this error occur and how can I get around it?
I know that this could be worked-around by doing the case in the gradient arithmetic expression, but my actual code is more complex and uses the Eigen3 matrix classes. Mostly I just want to understand this error.
Full code and compilation error here: http://ideone.com/nwAdTN