By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,483 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,483 IT Pros & Developers. It's quick & easy.

Changing a non-template class to a template class easily

P: n/a
gg
Let us say I have the following class -

class X {
int y;
};

Lot of other classes start using X.

Now, I want to make X a template class so that I can write,

template<class T>
class X {
T y;
};

How to minimize the impact on the client?

One way I can think of is, define the template class with a default
argument and with a new name,

template<class T = int>
class X1 {
T y;
};

and then do a typedef,

typedef X1<> X;

So the older clients can continue using the name X and they will get
the desired behavior.

The newer clients can use X1<float> etc.

Is this a good approach and are there any better ones?

Thanx,
gg

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
gg wrote:
Let us say I have the following class -

class X {
int y;
};

Lot of other classes start using X.

Now, I want to make X a template class so that I can write,

template<class T>
class X {
T y;
};

How to minimize the impact on the client?

One way I can think of is, define the template class with a default
argument and with a new name,

template<class T = int>
class X1 {
T y;
};

and then do a typedef,

typedef X1<> X;
There is no need for the default template argument. You could simply
say

typedef X1<int> X;
So the older clients can continue using the name X and they will get
the desired behavior.

The newer clients can use X1<float> etc.

Is this a good approach and are there any better ones?


Looks fine to me.

V
Jul 23 '05 #2

P: n/a


gg schreef:
Let us say I have the following class -

class X {
int y;
};

Lot of other classes start using X.

Now, I want to make X a template class so that I can write,

template<class T>
class X {
T y;
};

How to minimize the impact on the client?


One solution I've used (besides the obvious typedef) is inheritance:
template<class T>
class generic_X { /*** };

class X : public generic_X<int> { /* backwards-compatibility stuff */
};

In some cases, when the difference is too big, private inheritance
can be the better solution.

HTH,
Michiel Salters

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.