C
Christof Warlich
Hi,
I just learned and played around a bit with partial template
specialization, e.g. specializing for all pointers:
#include <iostream>
template<typename T> class MyClass {
public:
void someMember() {
std::cout << "No pointer.\n";
}
};
template<typename T> class MyClass<T *> {
public:
void someMember() {
std::cout << "Pointer.\n";
}
};
int main(void) {
MyClass<int *> pointer1;
MyClass<char *> pointer2;
MyClass<int> noPointer;
pointer1.someMember();
pointer2.someMember();
noPointer.someMember();
}
But what I really need is a partial specialization for a specific base
class and all its derived classes, i.e. something like:
#include <iostream>
class Base {};
class Derived: public Base {};
class Any {};
template<typename T> class MyClass {
public:
void someMember() {
std::cout << "Anything else.\n";
}
};
template<typename T> class MyClass<T: public Base> { // fantasy syntax
public:
void someMember() {
std::cout << "Class Base or any derived class of Base.\n";
}
};
int main(void) {
MyClass<Base> base;
MyClass<Derived> derived;
MyClass<Any> any;
base.someMember();
derived.someMember();
any.someMember();
}
Is there any way to achieve something like this?
Thanks for any help,
Christof
I just learned and played around a bit with partial template
specialization, e.g. specializing for all pointers:
#include <iostream>
template<typename T> class MyClass {
public:
void someMember() {
std::cout << "No pointer.\n";
}
};
template<typename T> class MyClass<T *> {
public:
void someMember() {
std::cout << "Pointer.\n";
}
};
int main(void) {
MyClass<int *> pointer1;
MyClass<char *> pointer2;
MyClass<int> noPointer;
pointer1.someMember();
pointer2.someMember();
noPointer.someMember();
}
But what I really need is a partial specialization for a specific base
class and all its derived classes, i.e. something like:
#include <iostream>
class Base {};
class Derived: public Base {};
class Any {};
template<typename T> class MyClass {
public:
void someMember() {
std::cout << "Anything else.\n";
}
};
template<typename T> class MyClass<T: public Base> { // fantasy syntax
public:
void someMember() {
std::cout << "Class Base or any derived class of Base.\n";
}
};
int main(void) {
MyClass<Base> base;
MyClass<Derived> derived;
MyClass<Any> any;
base.someMember();
derived.someMember();
any.someMember();
}
Is there any way to achieve something like this?
Thanks for any help,
Christof