C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can template specialization be based on a base class?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jay
Guest





PostPosted: Tue Mar 22, 2005 2:01 am    Post subject: Can template specialization be based on a base class? Reply with quote



I'd like to change the behaviour of a template class if the template
parameters are derived from a certain class e.g.


class Base
{
....
};

class Derived1 : public Base
{
....
};

class Derived2 : public Base
{
....
};

template <class T> class MyTemplateClass
{
public:
void f () {...} ;
};


so what I'd like to do is something like:

template <> class MyTemplateClass <derived from Base>
{
public:
void f () {/* do something different */ };
}

So far the nearest I've got is to do run-time checking i.e.

template <class T> void MyTemplateClass<T>::f ()
{
Base* b = dynamic_cast <Base*> (this);
if (b)
{
...
}
}

I'm not sure how much a compiler would optimize this and I'd rather not
modify the original template class if possible.

Any better ways of doing this?

Thanks,

Jay.





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jonathan Turkanis
Guest





PostPosted: Tue Mar 22, 2005 9:15 am    Post subject: Re: Can template specialization be based on a base class? Reply with quote



Jay wrote:
Quote:
I'd like to change the behaviour of a template class if the template
parameters are derived from a certain class e.g.

You can use boost::is_base_and_derived and boost::enable_if, as described here:

http://www.boost.org/libs/utility/enable_if.html (section 3.1)
http://www.boost.org/libs/type_traits/index.html#relationships

Jonathan



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Tue Mar 22, 2005 9:17 am    Post subject: Re: Can template specialization be based on a base class? Reply with quote



In article <d1neb6$bk$1 (AT) nyytiset (DOT) pp.htv.fi>, Jay
<jay (AT) aleka (DOT) freeserve.co.uk> wrote:

Quote:
I'd like to change the behaviour of a template class if the template
parameters are derived from a certain class e.g.


class Base
{
...
};

class Derived1 : public Base
{
...
};

class Derived2 : public Base
{
...
};

template <class T> class MyTemplateClass
{
public:
void f () {...} ;
};


so what I'd like to do is something like:

template <> class MyTemplateClass <derived from Base
{
public:
void f () {/* do something different */ };
}

So far the nearest I've got is to do run-time checking i.e.

template {
Base* b = dynamic_cast <Base*> (this);
if (b)
{
...
}
}

I'm not sure how much a compiler would optimize this and I'd rather not
modify the original template class if possible.

Any better ways of doing this?

Thanks,

Non abiguous interitence boost::is_complatable<T *,Base *> or

is_base_and_derived<T,Base> should provide a compile time type and
value
that is different if T is derived from Base and when it is not. So a
simple solution is tag dispatching via a bool template parameter of a
base class and using a struct to inherit the proper base class, the
struct only has forwarding constructors that are needed,

// the general class
template <class T,bool B> class MyTemplateBase
{
public:
static const bool general = true;
void f(){};
};

// this is the specialize class.
template <class T> class MyTemplateBase<T,true>
{
public:
static const bool general = false;
void f();
};

template <class T>
struct MyTemplate: MyTemplateBase
<
T,

boost::is_compatible
<
T *,
Base *
Quote:
::value

{

// forwarding constructors here
};

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ryan Gallagher
Guest





PostPosted: Tue Mar 22, 2005 9:24 am    Post subject: Re: Can template specialization be based on a base class? Reply with quote

Jay wrote:
Quote:
I'd like to change the behaviour of a template class if the template
parameters are derived from a certain class

Check out boosts type_traits library: www.boost.org. Sorry, but you're
going to have to do a bit of metaprogramming for this (just a bit Wink.
boost::enable_if can be used to select which member functions make it
into your function template's instance. Something like (not compiled):

#include <boost/type_traits/is_base_and_derived.hpp>
#include <boost/enable_if.hpp>

template<class T>
boost::enable_if<boost::is_base_and_derived void>::type
MyTemplateClass<T>::f()
{
// do something special for types derived from Base
}

HTH,

-Ryan


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Tue Mar 22, 2005 10:47 pm    Post subject: Re: Can template specialization be based on a base class? Reply with quote

Jay <jay (AT) aleka (DOT) freeserve.co.uk> wrote:

Quote:
I'm not sure how much a compiler would optimize this and I'd rather not
modify the original template class if possible.

Any better ways of doing this?

There was recently a similar (I believe) question and a solution regarding
a template specialization deriving a primary template:
http://groups-beta.google.com/group/comp.lang.c++.moderated/msg/eb7a5e89f9bcf7b4

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.