Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > partial templat...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 8 Topic 43929 of 48022
Post > Topic >>

partial template specialization for all derived cl*****

by Christof Warlich <cwarlich@[EMAIL PROTECTED] > Mar 15, 2008 at 07:12 PM

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 cl*****, 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
 




 8 Posts in Topic:
partial template specialization for all derived classes
Christof Warlich <cwar  2008-03-15 19:12:00 
Re: partial template specialization for all derived classes
Jeff Schwab <jeff@[EMA  2008-03-15 11:47:19 
Re: partial template specialization for all derived classes
Jeff Schwab <jeff@[EMA  2008-03-15 11:58:16 
Re: partial template specialization for all derived classes
Christof Warlich <cwar  2008-03-16 17:41:39 
Re: partial template specialization for all derived classes
Barry <dhb2000@[EMAIL   2008-03-15 19:55:13 
Re: partial template specialization for all derived classes
Barry <dhb2000@[EMAIL   2008-03-15 21:48:36 
Re: partial template specialization for all derived classes
Jeff Schwab <jeff@[EMA  2008-03-16 01:02:12 
Re: partial template specialization for all derived classes
Christof Warlich <cwar  2008-03-16 18:15:02 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:18:54 CDT 2008.