473,395 Members | 1,516 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,395 software developers and data experts.

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
Jul 19 '05 #1
3 1840

"Norman Evanson" <no************@theseed.net> wrote in message news:4e**************************@posting.google.c om...
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.
Jul 19 '05 #2
Norman Evanson wrote:
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 )
}

...
};

Jul 19 '05 #3
Faz
"Gianni Mariani" <gi*******@mariani.ws> wrote
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 <int Iother>
foo & operator add( const foo<T,Iother> & other )
{
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<T,"<<I<<">
Foo<T,"<<I<<">::operator+(Foo<T,"<<I<<">rhs)\n";
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 Foo<T,"<<X+I<<"> Foo<T,"<<I<<">::op+(Foo<T,"<<X<<">&
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 Foo<T,"<<I<<">::op+(Foo<T,"<<X<<">& rhs)\n";
// 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: " <<float2_2.size<<'\n';

// //call int Foo<float,2>::operator+<3>(const Foo<float,3>& rhs)
// int i23 = float2 + float3;
// cout << "float2 + float3 has value: " <<i23<<'\n';

//call Foo<float,2+3> Foo<float,2>::operator+<3>(const Foo<float,3>&
rhs)
Foo<float,2+3> float23 = float2 + float3;
cout << "float2 + float3 has Foo size: " <<float23.size<<'\n';

// Can't do this in presence of usual op+ ..
// //try to call Foo<float,2+2> Foo<float,2>::operator+<2>(const
Foo<float,2>& rhs)
// Foo<float,4> float4 = float2+float2; //Clashes with Foo<float,2>
Foo<float,2>::op+(..)
// cout << "float2 + float2 has Foo size: " <<float4.size<<'\n';
return 0;
}

Jul 19 '05 #4

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

Similar topics

2
by: brice | last post by:
Hello, I am using using the following code to transform a memo xml file. I am using Internet Explorer 6.0 to transform and view the file as HTML. <!-- ***** BEGIN XML ***** --> <?xml...
4
by: Kevin Dean | last post by:
I'm trying to create an XSL transformation that will strip out development-specific attributes from deployment descriptors and other XML files. I have already successfully done so with web.xml but...
2
by: Anna | last post by:
Hi all. I am trying to write a stylesheet that will structure the input HTML file in the following way: For each heading of level n it needs to enclose the heading and all its content until the...
1
by: bjam | last post by:
Hi, today I was able to abstract out into a separate xsl file a template that I specifically perform a call templates for, this worked with import no problem. However, when trying to do the same...
2
by: Soren Kuula | last post by:
Hi, Suppose I have: <xsl:template match="foo"/> <xsl:template match="foo"/> -- is that an error (same priority)? (my precessor reports nothing, buts as far as I recall it doesn't really...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
1
by: aidy | last post by:
Hi, I have created this XML for a test log <SNIP> <?xml version="1.0"?> <?xml-stylesheet type='text/xsl' href='show.xsl'?> <testresults> <test id="TESTID_10"> <teststatus>pass</teststatus>
2
by: tentstitcher | last post by:
Hello: I am stumped on what I belive to be a namespace issue. The XSL, source XML, and result XML are included below. The problem is that the template to match "getBalances" is never invoked by...
7
by: tthunder | last post by:
Hi @all, I have a trivial question, but I cannot find any answer :( <xsl:template match="note"> <b>Note:</b> </xsl:template> <xsl:template match="note"> <p><!-- CALL <xsl:template...
1
by: Joe Strout | last post by:
Wow, this was harder than I thought (at least for a rusty Pythoneer like myself). Here's my stab at an implementation. Remember, the goal is to add a "match" method to Template which works like...
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
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
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
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.