By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Partial template instantiation?

P: 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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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: 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?
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
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?


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
Jul 22 '05 #4

P: n/a
Ali Cehreli wrote:
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)
{}
};


Yes, but this has the unfortunate side effect that Point<float, N> is
not the same class as FloatPoint<N>.
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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.