Re: Specializing a template for a base and all its derived classes
Am 03.03.2012 21:13, schrieb Seungbeom Kim:
I have a class template P that holds a pointer to various types, and
I'd like to make an action customizable per the pointed-to type. I wrote:
template<typename T>
void action(T* ptr) { ... } // default
template<typename T>
void P<T>::f() const {
...
if (...) action(ptr);
...
}
Then I wanted to specialize action for specific class hierarchies.
What is the best way?
As you may already have guessed, the best way would be to use
concept-constrained template ;-)
In fact, concepts would have allowed such refinement without falling
back to less robust techniques.
First I thought this would work:
template<>
void action(Animal* ptr) { ... }
template<>
void action(Fruit* ptr) { ... }
but it doesn't; e.g. when ptr is a Dog*, action(ptr) will choose
the generic version, not the one specialized for Animal*, even though
a Dog is an Animal.
This is to be expected, because explicit specialization does not respect
inheritance. Neither does overloading, as in
void action(Animal* ptr) { ... }
void action(Fruit* ptr) { ... }
The generic template will just be the better match, except if the
arguments types where Animal or Fruit exactly.
I've even tried:
template<typename T>
void action(
enable_if<is_base_of<Animal, T>::value, T>* ptr
) { ... }
but in vain.
I've seen specialization techniques, but all that I've seen depended on
specialization on is_base_of<...>::value, thus requiring the designer
of the Holder class to be aware of specific specializations in advance;
e.g. whether T is an Animal or not.
template<typename T, bool isAnimal>
struct action_t { ... };
template<typename T>
struct action_t<T, true> { ... };
template<typename T>
void action(T* ptr) {
action_t<T, is_base_of<Animal, T>::value>()(ptr);
}
This works if the design of action_t, which is a part of the interface
of Holder, is tightly coupled with that of the Animal hierarchy, but it
doesn't help if the designer of Holder shouldn't be aware of any such
hierarchy in advance; he or she cannot provide bool template parameters
for all possible specializations.
Are there any good ways to achieve what I want?
One possible way would be the following one:
Ensure that your primary template is a slightly worse match (even in the
absence of constraints), then add constrained overloads. Here is an
example of what I'm thinking of:
#include <type_traits>
#include <iostream>
struct Animal {};
struct Dog : Animal {};
struct Fruit {};
struct Apple : Fruit {};
struct Other {};
template<class T>
typename std::enable_if<std::is_pointer<T>::value>::type
do_action(T ptr) { std::cout << "generic" << std::endl; }
template<class T>
inline void action(T* ptr) { do_action(ptr); }
template<class T>
typename std::enable_if<std::is_base_of<Animal, T>::value>::type
do_action(T* ptr) { std::cout << "animals" << std::endl; }
template<class T>
typename std::enable_if<std::is_base_of<Fruit, T>::value>::type
do_action(T* ptr) { std::cout << "fruits" << std::endl; }
int main()
{
Dog d;
action(&d);
Apple a;
action(&a);
Other o;
action(&o);
}
Note that this can still lead to problems if you want to add further
overloads that are even more refined, e.g. for all dogs instead for all
animals.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]