On 3=D4 16=C8=D5, =C9=CF=CE=E72=CA=B158=B7=D6, Jeff Schwab <j...@schwabcen=
Jeff Schwab wrote:
Christof Warlich wrote:
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?
Use a boolean test like boost::is_base_of to get a bool. Use the bool
as an argument to a separate template, which you may specialize for
either the true or false case.
http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_t...
I just realized is_base_of is in tr1. (And there was much rejoicing.)
#include <iostream>
#include <tr1/type_traits>
struct base { };
struct derived: base { };
struct any { };
template<bool is_subclass>
struct my_class_impl {
void some_member() const {
std::cout << "Anything else.\n";
}
};
template<>
struct my_class_impl<true> {
void some_member() const {
std::cout << "Class base or subclass of base.\n";
}
};
template<typename T>
struct my_class:
my_class_impl<std::tr1::is_base_of<base, T>::value > { };
int main() {
my_class<base>().some_member();
my_class<derived>().some_member();
my_class<any>().some_member();
making it the way the OP did.