473,408 Members | 2,888 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,408 software developers and data experts.

about the template pragramming

Dear All,

Assume I have a class template of multi-dimension point as follows:

template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

private:
double m_coordinate[nDim];
};

But for different dimetion points, their constructor are different such
as
Point<1>(double);
Point<2>(double, double);
Point<3>(double, double, double);

So we can specialize the classes to implement the constructors. But
this way need to rewrite the common code such as "double
GetCoordinate(int)". Is there any easy way to implement them? What I
mean is that just specializing those constructors rather than
specialize the total class.

Thanks for your help!

Shuisheng

Sep 21 '06 #1
6 1393
shuisheng wrote:
Dear All,

Assume I have a class template of multi-dimension point as follows:

template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

private:
double m_coordinate[nDim];
};

But for different dimetion points, their constructor are different such
as
Point<1>(double);
Point<2>(double, double);
Point<3>(double, double, double);

So we can specialize the classes to implement the constructors. But
this way need to rewrite the common code such as "double
GetCoordinate(int)". Is there any easy way to implement them? What I
mean is that just specializing those constructors rather than
specialize the total class.
What about inheriting from an underlying struct/class that just manages the
common parts:

template < typename ValueType, unsigned int Dim >
class point_storate_policy {

ValueType data [ Dim ];

public:

static unsigned int const size = Dim;

typedef ValueType value_type;
typedef value_type * pointer;
typedef value_type & reference;

pointer begin ( void ) {
return data;
}

pointer end ( void ) {
return data + size;
}

reference operator[] ( unsigned int i ) {
return ( data[i] );
}

};

template < unsigned int Dim >
class point;

template <>
struct point<1: public point_storate_policy< double, 1 {

point( double x ) {
(*this)[0] = x;
}

};

template <>
struct point<2: public point_storate_policy< double, 2 {

point( double x, double y ) {
(*this)[0] = x;
(*this)[1] = y;
}

};

#include <iostream>

int main ( void ) {
point<2p ( 0.1, 0.4 );
std::cout << p[0] << " " << p[1] << '\n';
}
Best

Kai-Uwe Bux
Sep 21 '06 #2
shuisheng wrote:
Dear All,

Assume I have a class template of multi-dimension point as follows:

template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

private:
double m_coordinate[nDim];
};

But for different dimetion points, their constructor are different such
as
Point<1>(double);
Point<2>(double, double);
Point<3>(double, double, double);

So we can specialize the classes to implement the constructors. But
this way need to rewrite the common code such as "double
GetCoordinate(int)". Is there any easy way to implement them? What I
mean is that just specializing those constructors rather than
specialize the total class.

Thanks for your help!

If you really really want to do this, you can use a feature of the
template function instantiation that allows you to specialize the
constructor.

// This template will have an error if L1 & L2 are not equal
template <int L1, int L2 >
struct AssertEqual
{
char foo[ (L1==L2)?1:-1 ]; // error if L1 != L2
};
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDim,1& = AssertEqual<nDim,1>() )
{
m_coordinate[0] = v0;
}

Point( double v0, double v1, AssertEqual<nDim,2& =
AssertEqual<nDim,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}

Point( double v0, double v1, double v2, AssertEqual<nDim,3& =
AssertEqual<nDim,3>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
m_coordinate[2] = v2;
}

private:
double m_coordinate[nDim];
};
Point<3x3(1,2,3);
//Point<3x2AssertEqual(1,2); // compile time error
Point<2x2(1,2);
Point<1x1(1);
Sep 21 '06 #3
Thank you for your good answer. I am just wondering where I put the
private member varaibels? In the base class or the individuan derived
class? Thanks.

Sep 21 '06 #4
You idea is so nice. I just still have a question:

What does "AssertEqual<nDim,1& = AssertEqual<nDim,1>() " mean?
AssertEqual<nDim,1>() is an object. And AssertEqual<nDim,1is a
class.
What is Class & = Object?

Thanks.
Gianni Mariani wrote:
shuisheng wrote:
Dear All,

Assume I have a class template of multi-dimension point as follows:

template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

private:
double m_coordinate[nDim];
};

But for different dimetion points, their constructor are different such
as
Point<1>(double);
Point<2>(double, double);
Point<3>(double, double, double);

So we can specialize the classes to implement the constructors. But
this way need to rewrite the common code such as "double
GetCoordinate(int)". Is there any easy way to implement them? What I
mean is that just specializing those constructors rather than
specialize the total class.

Thanks for your help!


If you really really want to do this, you can use a feature of the
template function instantiation that allows you to specialize the
constructor.

// This template will have an error if L1 & L2 are not equal
template <int L1, int L2 >
struct AssertEqual
{
char foo[ (L1==L2)?1:-1 ]; // error if L1 != L2
};
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDim,1& = AssertEqual<nDim,1>() )
{
m_coordinate[0] = v0;
}

Point( double v0, double v1, AssertEqual<nDim,2& =
AssertEqual<nDim,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}

Point( double v0, double v1, double v2, AssertEqual<nDim,3& =
AssertEqual<nDim,3>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
m_coordinate[2] = v2;
}

private:
double m_coordinate[nDim];
};
Point<3x3(1,2,3);
//Point<3x2AssertEqual(1,2); // compile time error
Point<2x2(1,2);
Point<1x1(1);
Sep 21 '06 #5
At last, I see your point. SO NICE ! ! !

"AssertEqual<nDim,1& = AssertEqual<nDim,1>()" is an argument with a
default value, right? I think I am still a c++ novice.

shuisheng wrote:
You idea is so nice. I just still have a question:

What does "AssertEqual<nDim,1& = AssertEqual<nDim,1>() " mean?
AssertEqual<nDim,1>() is an object. And AssertEqual<nDim,1is a
class.
What is Class & = Object?

Thanks.
Gianni Mariani wrote:
shuisheng wrote:
Dear All,
>
Assume I have a class template of multi-dimension point as follows:
>
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}
>
private:
double m_coordinate[nDim];
};
>
But for different dimetion points, their constructor are different such
as
Point<1>(double);
Point<2>(double, double);
Point<3>(double, double, double);
>
So we can specialize the classes to implement the constructors. But
this way need to rewrite the common code such as "double
GetCoordinate(int)". Is there any easy way to implement them? What I
mean is that just specializing those constructors rather than
specialize the total class.
>
Thanks for your help!

If you really really want to do this, you can use a feature of the
template function instantiation that allows you to specialize the
constructor.

// This template will have an error if L1 & L2 are not equal
template <int L1, int L2 >
struct AssertEqual
{
char foo[ (L1==L2)?1:-1 ]; // error if L1 != L2
};
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDim,1& = AssertEqual<nDim,1>() )
{
m_coordinate[0] = v0;
}

Point( double v0, double v1, AssertEqual<nDim,2& =
AssertEqual<nDim,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}

Point( double v0, double v1, double v2, AssertEqual<nDim,3& =
AssertEqual<nDim,3>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
m_coordinate[2] = v2;
}

private:
double m_coordinate[nDim];
};
Point<3x3(1,2,3);
//Point<3x2AssertEqual(1,2); // compile time error
Point<2x2(1,2);
Point<1x1(1);
Sep 21 '06 #6
shuisheng wrote:
At last, I see your point. SO NICE ! ! !

"AssertEqual<nDim,1& = AssertEqual<nDim,1>()" is an argument with a
default value, right? I think I am still a c++ novice.
Yes, correct.
>
shuisheng wrote:
>You idea is so nice. I just still have a question:

What does "AssertEqual<nDim,1& = AssertEqual<nDim,1>() " mean?
AssertEqual<nDim,1>() is an object. And AssertEqual<nDim,1is a
class.
What is Class & = Object?

Thanks.
Gianni Mariani wrote:
>>shuisheng wrote:
Dear All,

Assume I have a class template of multi-dimension point as follows:

template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

private:
double m_coordinate[nDim];
};

But for different dimetion points, their constructor are different such
as
Point<1>(double);
Point<2>(double, double);
Point<3>(double, double, double);

So we can specialize the classes to implement the constructors. But
this way need to rewrite the common code such as "double
GetCoordinate(int)". Is there any easy way to implement them? What I
mean is that just specializing those constructors rather than
specialize the total class.

Thanks for your help!

If you really really want to do this, you can use a feature of the
template function instantiation that allows you to specialize the
constructor.

// This template will have an error if L1 & L2 are not equal
template <int L1, int L2 >
struct AssertEqual
{
char foo[ (L1==L2)?1:-1 ]; // error if L1 != L2
};
template<int nDim>
class Point
{
public:
double GetCoordinate(int iDim)
{
return m_coordinate[iDim];
}

// define all the constuctors adding a default last parameter
// whose type will have an error if it is instantiated
Point( double v0, AssertEqual<nDim,1& = AssertEqual<nDim,1>() )
{
m_coordinate[0] = v0;
}

Point( double v0, double v1, AssertEqual<nDim,2& =
AssertEqual<nDim,2>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
}

Point( double v0, double v1, double v2, AssertEqual<nDim,3& =
AssertEqual<nDim,3>() )
{
m_coordinate[0] = v0;
m_coordinate[1] = v1;
m_coordinate[2] = v2;
}

private:
double m_coordinate[nDim];
};
Point<3x3(1,2,3);
//Point<3x2AssertEqual(1,2); // compile time error
Point<2x2(1,2);
Point<1x1(1);
Sep 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
1
by: suzy | last post by:
hi, i have created a aspx page template by creating a template.cs file which inherits from the page class. In this file I override the OnInit event and create a template in the form of a...
3
by: mca | last post by:
Hi everyone, I'm new to asp.net and i have a question about separating the html code from the programming code. i have an unknown numbers of entries in my table. I want to make a hyperlink...
8
by: sagi.perel | last post by:
I have tried to compile the following code on Win & Unix. Doesn't work on either. <----- CODE -----> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <vector> #include...
4
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; ...
17
by: JohnQ | last post by:
Is that I can approximate using my ideal of a language in it. On the flip side, what I dislike most about it is all the flak/protecting-the-golden-calf one gets when using it that way. I think a...
3
by: goodmen | last post by:
I think the boost::concept_check is too complex. Here is my solution about static interface and concept. The static interface can be used as function param. It is just a proxy of the real...
4
by: parag_paul | last post by:
hi All , I am giving the pseudo code about the problem I am talking about . I have a class hiearchy which looks like this. class A; template <class X1, class X2class B : public A{}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.