Connecting Tech Pros Worldwide Help | Site Map

Partial template instantiation?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 05:58 PM
Peter Ammon
Guest
 
Posts: n/a
Default Partial template instantiation?

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, 05:58 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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, 05:58 PM
Ali Cehreli
Guest
 
Posts: n/a
Default 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, 05:58 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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, 05:58 PM
Peter Ammon
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.