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 

Emulating virtual function templates

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





PostPosted: Tue Jun 20, 2006 2:44 pm    Post subject: Emulating virtual function templates Reply with quote



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! ]
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.