Re: Partial template instantiation?

From: Ali Cehreli (acehreli_at_yahoo.com)
Date: 08/16/04


Date: Mon, 16 Aug 2004 14:57:43 -0700

On Mon, 16 Aug 2004 14:23:00 -0700, Peter Ammon wrote:

> I have a template class:
>
> #include <cassert>
> #include <algorithm>
>
> template <class T, int N>
> class Point {
> private:
> T components[N];
>
> public:
> T& operator[](unsigned x) {
> assert(x < N);
> return components[x];
> }
>
> const T& operator[](unsigned x) const {
> assert(x < N);
> return components[x];
> }
>
> Point(const T* val) {
> std::copy(val, val + N, components);
> }
> };
>
> I would like to have another template template <int N> class FloatPoint
>
> where FloatPoint<N> is equivalent to Point<float, N>
>
> Is this doable? If not, what's the closest alternative?

You can inherit from Point<float, N>

template <int N>
class FloatPoint : public Point<float, N> {
    typedef Point<float, N> Base;
    
public:

    FloatPoint(const float * val)
        :
        Base(val)
    {}
};

int main()
{
    float const init[3] = { 1.0, 2.0, 3.0 };
    FloatPoint<3> f(init);
    std::cout << f[1] << '\n';
}

If I'm not mistaken, this will be unnecessary when template typedefs are
added to the language. (?)

Ali