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

Extending a class template

Let say I have a class template Pair representing an ordered pair, ie:

#v+
template<class T1, class T2>
class Pair {
T1 a;
T2 b;
public:
Pair() : a(), b() { }
Pair(const Pair<T1, T2&p) : a(p.a), b(p.b) { }
Pair(const T1 &A, const T2 &B) : a(A), b(B) { }

const T1 &left () { return a; }
const T2 &right() { return b; }
};
#v-

Now, if both types are the same I'd like to "add" another method (say)
areEqual() which compares both members (a and b). I could make a
specialization but it would require repeating all common members, ie:

#v+
template<class T>
class Pair<T, T{
T a, b;
public:
Pair() : a(), b() { }
Pair(const Pair<T&p) : a(p.a), b(p.b) { }
Pair(const T &A, const T &B) : a(A), b(B) { }

const T &left () { return a; }
const T &right() { return b; }
bool areEqual() { return a == b; }
};
#v-

Is it possible to some how "extend" a class template with new members
if some condition on template parameters is met?

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Nov 12 '06 #1
3 8527
Michal Nazarewicz wrote:
>
Is it possible to some how "extend" a class template with new members
if some condition on template parameters is met?
The simplest way to add that areEqual function is to make it a free
function:

template <class Ty>
bool areEqual(const Pair<Ty, Ty>&p)
{
return p.left() == p.right();
}

Of course, that approach might not work for other function. In general,
when you need two classes or templates to share capabilities, you
refactor the design: split the original one into two pieces, one derived
from the other, and reuse the base for your new one. For example:

template <class T1, class T2>
class Pair_base
{
private:
T1 a;
T2 b;
public:
Pair_base() : a(), b() {}
Pair_base(const Pair_base& other) : a(other.a), b(other.b) {}
Pair_base(const T1& a0, const T2& b0) : a(a0, b(b0) {}
const T1& left() const { return a; }
const T1& right() const { return b; }
};

template <class T1, class T2>
class Pair : public Pair_base<T1, T2>
{
typedef Pair_base<T1, T2MyBase;
public:
Pair() : MyBase() {}
Pair(const Pair& other) : MyBase(other) {}
Pair_base(const T1& a0, const T2& b0) : MyBase(a0, b0) {}
};

template <class Ty>
class Pair : public Pair_base<Ty, Ty>
{
typedef Pair_base<Ty, TyMyBase;
public:
Pair() : MyBase() {}
Pair(const Pair& other) : MyBase(other) {}
Pair_base(const T1& a0, const T2& b0) : MyBase(a0, b0) {}
bool areEqual() const { return left() == right(); }
};

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Nov 12 '06 #2

Michal Nazarewicz wrote:
Let say I have a class template Pair representing an ordered pair, ie:

#v+
template<class T1, class T2>
class Pair {
T1 a;
T2 b;
public:
Pair() : a(), b() { }
Pair(const Pair<T1, T2&p) : a(p.a), b(p.b) { }
Pair(const T1 &A, const T2 &B) : a(A), b(B) { }

const T1 &left () { return a; }
const T2 &right() { return b; }
};
#v-

Now, if both types are the same I'd like to "add" another method (say)
areEqual() which compares both members (a and b). I could make a
specialization but it would require repeating all common members, ie:

#v+
template<class T>
class Pair<T, T{
T a, b;
public:
Pair() : a(), b() { }
Pair(const Pair<T&p) : a(p.a), b(p.b) { }
Pair(const T &A, const T &B) : a(A), b(B) { }

const T &left () { return a; }
const T &right() { return b; }
bool areEqual() { return a == b; }
};
#v-

Is it possible to some how "extend" a class template with new members
if some condition on template parameters is met?
Have u seen the Boost MPL library, in particular the template 'is_same'
I think something like this could work

enable_if_c<is_same<T1,T2>,bool>::type areEqual() { return a == b; }

so that this function only comes into play if T1 and T2 are the same,
even if the line I have written above is incorrect I am sure it is very
close what will work.
Boost has some funcky stuff like that, for example is_base is another
very useful one.

Nov 12 '06 #3
this is one way:

template<class T, bool b>
struct pair_bs {};

template<class T>
struct pair_bs<T, true>
{
bool areEqual()
{
T * pT = static_cast<T*>(this);
return pT->left() == pT->right();
}
};

use type trait (boost for example)

template<class T1, class T2>
class Pair : public pair_bs<Pair<T1, T2>, ::boost::is_same<T1,
T2>::value>
{
T1 a;
T2 b;
public:
Pair() : a(), b() { }
Pair(const Pair<T1, T2&p) : a(p.a), b(p.b) { }
Pair(const T1 &A, const T2 &B) : a(A), b(B) { }

const T1 &left () { return a; }
const T2 &right() { return b; }
};
Nov 13 '06 #4

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

Similar topics

2
by: Lars Yencken | last post by:
Hello, I'm working on a project where we need to index files as we're writing them, which basically just involves determining how many bytes are output each time we write to a stream. What...
4
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on...
2
by: Barry Hynes | last post by:
G'Day folks, Have been working on this problem for quite some time and still no farther ahead. :( Here is my problem...bare with me i am very green :) I have to implement a Safe List,...
7
by: Paulustrious | last post by:
How do I extend a template class for a specific template data type? I am trying to achieve something like.... public class SomeItem{} public class SomeProcess < T> { } and now the extension ...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
13
by: interec | last post by:
I have some code in Java that I need to translate into C++. My Java code defines a class hierarchy as follows: // interface IA public interface IA { public abstract void doA(); } //...
6
by: jamesrjones | last post by:
I'm writing a template library that models the IEEE754(r) decimal arithmetic standard, and I'd like to provide numeric_limits<for my templates. The problem is that I'm not sure how to do this. ...
8
by: Floortje | last post by:
Hi i have been struggeling with this question for quite some time now. I have some helper classes that handle images (upload an image, create thumbnails and show a imagelist), links (add link,...
4
by: PapaLegba | last post by:
This is probably extremely simple, but I am not comfortable with templates. The Boost++ libraries define two templates which I wish to extend ... template< class MostDerived, class...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.