Connecting Tech Pros Worldwide Help | Site Map

Partial template instantiation?

Peter Ammon
Guest
 
Posts: n/a
#1: Jul 22 '05
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
Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Partial template instantiation?


* Peter Ammon:[color=blue]
>
> 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?[/color]

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

[color=blue]
> If not, what's the closest alternative?[/color]

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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Ali Cehreli
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Partial template instantiation?


On Mon, 16 Aug 2004 14:23:00 -0700, Peter Ammon wrote:
[color=blue]
> 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?[/color]

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
Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Partial template instantiation?


Peter Ammon wrote:[color=blue]
> 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?[/color]

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
Peter Ammon
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Partial template instantiation?


Ali Cehreli wrote:
[color=blue]
> On Mon, 16 Aug 2004 14:23:00 -0700, Peter Ammon wrote:
>
>[color=green]
>>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?[/color]
>
>
> 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)
> {}
> };
>[/color]

Yes, but this has the unfortunate side effect that Point<float, N> is
not the same class as FloatPoint<N>.
[color=blue]
> 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[/color]

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

-Peter
Closed Thread