 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jay Guest
|
Posted: Tue Mar 22, 2005 2:01 am Post subject: Can template specialization be based on a base class? |
|
|
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
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Tue Mar 22, 2005 9:17 am Post subject: Re: Can template specialization be based on a base class? |
|
|
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 *
// 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
|
Posted: Tue Mar 22, 2005 9:24 am Post subject: Re: Can template specialization be based on a base class? |
|
|
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 .
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
|
Posted: Tue Mar 22, 2005 10:47 pm Post subject: Re: Can template specialization be based on a base class? |
|
|
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 |
|
 |
|
|
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
|
|