Connecting Tech Pros Worldwide Help | Site Map

Partial template instantiation?

  #1  
Old July 22nd, 2005, 06:58 PM
Peter Ammon
Guest
 
Posts: n/a
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
  #2  
Old July 22nd, 2005, 06:58 PM
Alf P. Steinbach
Guest
 
Posts: n/a

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?
  #3  
Old July 22nd, 2005, 06:58 PM
Ali Cehreli
Guest
 
Posts: n/a

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
  #4  
Old July 22nd, 2005, 06:58 PM
Victor Bazarov
Guest
 
Posts: n/a

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
  #5  
Old July 22nd, 2005, 06:58 PM
Peter Ammon
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the correct syntax for a friend with template parameters? Andy Champ answers 2 July 10th, 2008 08:15 PM
Template specialization and partial specialization Alfonso Morra answers 4 November 17th, 2005 05:20 PM
template instantiation syntax Faheem Mitha answers 1 October 30th, 2005 10:05 PM
template instantiation error Hunter Hou answers 7 July 22nd, 2005 01:18 PM