473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 22 '05 #1
4 1728
* 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
8
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
2
by: Martin Gernhard | last post by:
Hi, I'm trying to use expression templates to calculate values at compile time. Doing it with just one parameter is no problem: ///Begin Listing 1 #include <iostream> using namespace std; ...
5
by: dascandy | last post by:
The following program produces no warnings, no errors, but doesn't do what I expect it to. I expect it to produce a warning or error. What does the standard say about this and/or what should it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.