Emulating virtual function templates
Hallo,
first of all, let me state that I *know* that member function templates
cannot be virtual; however I need to somehow achieve something as close
to the following as possible:
// Beware! Invalid C++
struct base {
template <typename T>
virtual void set(T const & t) = 0;
};
struct env {
template <typename T> void set(T const & t) { p->set(t); }
base * p;
};
What I could come up with is the following:
struct proxy_base {
virtual ~proxy_base() {}
};
template <typename T> struct proxy : proxy_base {
virtual void set(T const &) = 0;
};
struct base2 {
virtual proxy_base * get() = 0;
};
struct env2 {
template <typename T> void set(T const & t) {
proxy_base * pbp = p->get();
assert(dynamic_cast<proxy<T> *>(pbp));
static_cast<proxy<T> *>(pbp)->set(t);
}
base2 * p;
};
Is there any better solution?
Thank you,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]