Let's say you have a vector class with the length and type of the template - i.e. vec<2,float>. They can also be nested - vec<2,vec<2,vec<2,float> > >or vec<2,vec<2,float> >. You can calculate how deeply one of these vectors is embedded:
template<typename T>
inline int depth(const T& t) { return 0; }
template<int N, typename T>
inline int depth(const vec<N,T>& v) { return 1+depth(v[0]); }
The problem is that you won’t know how deep it is to runtime, but you may need to know the depth at compile time to do something like this:
template<int N, typename T, int M, typename U>
inline vec<N,T> operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
return v1 + coerce(v2,v1);
}
template<int N, typename T, int M, typename U>
inline vec<M,U> operator +(const vec<N,T>& v1, const vec<M,U>& v2) {
return coerce(v1,v2) + v2;
}
You can't just insert the if statement because (a), which has a deeper effect on the type of return value, and (b) coerce () generates a build error if you try to force a nested vector to a less nested one.
Is it possible to do something like this, or did I push the framework of C ++ templates?