M
Michal Nazarewicz
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?
#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?