 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Norman Evanson Guest
|
Posted: Fri Sep 26, 2003 3:08 pm Post subject: Template Mix 'n' Match |
|
|
Hi Folks.
Many thanks for the solution to this problem
Given a templated class
template <class T, int I> foo { /* ....*/ }
how do I go about( if indeed it's possible) adding (for example) a
foo<T,x> to a foo<T,y> where x!=y
Many thanks
Norman
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Sep 26, 2003 3:21 pm Post subject: Re: Template Mix 'n' Match |
|
|
"Norman Evanson" <norman.evanson (AT) theseed (DOT) net> wrote
| Quote: | how do I go about( if indeed it's possible) adding (for example) a
foo<T,x> to a foo<T,y> where x!=y
foo<T,x> + foo<T.y>? |
You're going to have to explain in more detail wh at you are trying to do.
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Fri Sep 26, 2003 4:18 pm Post subject: Re: Template Mix 'n' Match |
|
|
Norman Evanson wrote:
| Quote: | Hi Folks.
Many thanks for the solution to this problem
Given a templated class
template <class T, int I> foo { /* ....*/ }
how do I go about( if indeed it's possible) adding (for example) a
foo<T,x> to a foo<T,y> where x!=y
Many thanks
Norman
|
you mean somthing like :
template <class T, int I>
struct foo
{
...
foo( stuff_type v );
template <int Iother>
foo & operator add( const foo<T,Iother> & other )
{
return foo( this.suff + other.stuff )
}
...
};
|
|
| Back to top |
|
 |
Faz Guest
|
Posted: Tue Nov 11, 2003 6:46 pm Post subject: Re: Template Mix 'n' Match |
|
|
"Gianni Mariani" <gi2nospam (AT) mariani (DOT) ws> wrote
| Quote: | Norman Evanson wrote
[]
Given a templated class
template <class T, int I> foo { /* ....*/ }
how do I go about( if indeed it's possible) adding (for example) a
foo<T,x> to a foo<T,y> where x!=y
[]
you mean somthing like :
template <class T, int I
struct foo
{
...
foo( stuff_type v );
template
foo & operator add( const foo
{
return foo( this.suff + other.stuff )
}
...
};
|
That didn't compile (But its more elegant than the 4 parameter template I
first cooked up :-)
BTW: I see no natural choice for the type of foo<T,x> + foo<T,y>.
Symmetry speaks against either foo<T,x> or foo<T,y> unless x==y.
Where x and y differ, Foo<T, x+y> and simply int seem to make
sense as the return type for addition. Overloading restrictions
mean you can only choose one, of course.
Try this. It compiled on bcc32 v5.5 and g++ 2.95 under cygwin/win2k.
(I no longer trust just one compiler
-Fazl
#include <iostream>
using std::cout;
template <class T, int I> struct Foo {
T m_t; //value set by ctor
int size; //could make static
explicit Foo( T t ) : m_t(t), size(I) {}
//Usual op+
Foo operator+( const Foo& rhs ){
cout <<"In usual Foo
Foo<T,"<
Foo<T,I> sum = *this;
//'add' rhs to sum, however you want to
return sum;
}
//Parameterise int argument.. returning Foo<T,size+rhs.size>:
template<int X> Foo<T,X+I> operator+(const Foo<T,X>& rhs){
cout <<"In Foop+(Foo<T,"<
rhs)n";
T tpart = m_t + rhs.m_t; //or whatever you want with the T parts
Foo<T,X+I> sum(tpart); //can't assign from *this as different
class unless X==I
return sum;
}
// // ..returning int:
// template<int X> int operator+(const Foo<T,X>& rhs){
// cout <<"In int Foop+(Foo<T,"<
// return size + X;
// }
};
int main(){
Foo<float, 2> float2(0.);
Foo<float, 3> float3(0.);
Foo<double,3> double3(0.);
//call usual op+
Foo<float,2> float2_2 = float2+float2;
cout << "float2 + float2 has Foo size: " <
// //call int Foo
// int i23 = float2 + float3;
// cout << "float2 + float3 has value: " <
//call Foo
rhs)
Foo<float,2+3> float23 = float2 + float3;
cout << "float2 + float3 has Foo size: " <
// Can't do this in presence of usual op+ ..
// //try to call Foo
Foo<float,2>& rhs)
// Foo<float,4> float4 = float2+float2; //Clashes with Foo<float,2>
Foo<float,2>: p+(..)
// cout << "float2 + float2 has Foo size: " <
return 0;
}
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|