I am trying to specialise some geometrical functions depending on 2D or 3D, specified by a template parameter. Best if I include some (very broken) code for a toy version of the problem:
template <typename T, int d>
class Point
{
public:
int x;
int y;
int z;
T add ()
{
return T(0);
}
template <>
T add <T, 2> ()
{
return x + y;
}
template <>
T add <T, 3> ()
{
return x + y + z;
}
};
This code fails miserably to compile. I've tried lots of different combinations of template parameter formats and class definitions, and cannot find a way to do function specialisation on 'd' whilst leaving the 'T' general.
In my actual solution I'm trying to calculate things like gradients, curvature, interpolation, etc, specialised for 2D or 3D cases. Some things, like gradient calculations, can simply use the 'd' parameter to limit for-loop iterations. Others, like interpolation, require a separate function for 2D and 3D.
Any hints much appreciated!