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



Relevant Pages

  • Simple script annimation problem usig swing
    ... To change this template, ... public void destroy{ ... public state cstate; ... public static int respawn = 20; ...
    (comp.lang.java.programmer)
  • Re: Simple script annimation problem usig swing
    ... To change this template, ... public void destroy{ ... public state cstate; ... public static int respawn = 20; ...
    (comp.lang.java.programmer)
  • Re: Replacing merge fields in headers/footers
    ... database, and a newly populated document can be requested at any time. ... to my way of thinking is to have a merge template ... I'm using exactly the same code to populate the fields in the ... >> private FileStream PopulateTemplateDocument(int theFileID, ...
    (microsoft.public.word.vba.general)
  • implementation details
    ... template class ) ... Retrieve then I'd like to implement some sort of overrun check. ... BUFFER(); ... unsigned int retrieve_count; ...
    (comp.lang.cpp)
  • friend ostream& operator<< (ostream&, Array<T>&);
    ... Array(int itsSize = DefaultSize); ... int GetSize() const ... make sure the function template has already been declared and add after ... // implement the Constructor ...
    (alt.comp.lang.learn.c-cpp)