Partial template instantiation?

P

Peter Ammon

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?

Thanks,
-Peter
 
A

Alf P. Steinbach

* Peter Ammon:
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?

Not before we get template typedef's in C++0x.

If not, what's the closest alternative?

E.g., off the cuff,


#include <cassert>
#include <algorithm>

template< typename T, unsigned N >
struct Space
{
class Point
{
private:
T components[N];

public:
T& operator[]( unsigned x )
{
assert( x < N );
return components[x];
}

T const& operator[]( unsigned x ) const
{
assert( x < N );
return components[x];
}

Point( T const* val )
{
std::copy( val, val + N, components );
}
};
};

template< unsigned N >
struct FloatSpace
{
typedef Space<float, N>::point Point;
};

int main()
{
Space<float, 3>::point point1;
FloatSpace<3>::point point2;
}
 
A

Ali Cehreli

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
 
V

Victor Bazarov

Peter said:
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?

Template typedefs are coming in a couple of years, but I guess you don't
want to wait and need a solution now. A work-around could be

template<int N> struct FloatPoint // this serves as a wrapper
{ typedef Point<float,N> impl; };

Now you can use FloatPoint<N>::impl as you would 'FloatPoint<N>':

FloatPoint<3>::impl myPoint_float_3;

Victor
 
P

Peter Ammon

Ali said:
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)
{}
};

Yes, but this has the unfortunate side effect that Point<float, N> is
not the same class as FloatPoint said:
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

So that's what I'm hurting for. Thanks.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top