member function and const member function as template arguments

member function and const member function as template arguments

Post by Cristi Be » Fri, 16 Sep 2005 18:15:09


I wrote a dispatcher for Lua in which I have a class with a function
like this one:

class CLuaDispatcher
{
//...
template<typename Function>
void RegisterFunction(Function function)
{
//....
PushMemberDispatcher(function);
//....
}
void
//...
};

I obtain the return type, the callee class and the number and type of
parameters like this:

template<typename Callee, typename RT>
inline void PushMemberDispatcher(lua_State* state, RT (Callee::*)()) {
//...}
template<typename Callee, typename RT, typename P1>
inline void PushMemberDispatcher(lua_State* state, RT (Callee::*)(P1))
{ //... }
template<typename Callee, typename RT, typename P1, typename P2>
inline void PushMemberDispatcher(lua_State* state, RT (Callee::*)(P1,
P2)) { //... }
//etc.

I would wish to call the function in the following way:

CLuaDispatcher ld;
ld.RegisterFunction(&CSomeClass::MemeberFunction);

But this only works for non-const not-overloaded functions. If the
function is const or overloaded I must explicitly cast it to it's type
(for ex. ld.RegisterFunction((void (CSomeClass::*
const)())&CSomeClass::MemeberFunction);
I realize that I must do this for overloaded functions, but is there a
way to avoid the casting for const functions?


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

member function and const member function as template arguments

Post by Greg Herli » Fri, 16 Sep 2005 22:57:57


Yes, just declare a set of function templates that accept const member
function pointers to match the existing function templates that accept
non-const member function pointers.

For example:

template<typename Callee, typename RT, typename P1>
inline void
PushMemberDispatcher(lua_State* state, RT (Callee::*)(P1) const)

would match a pointer to a const member function with an arity of one.

It may be helpful to consult the implementation of std::mem_fun (found
in the header <functional>) since it too has to accept both const and
non-const member function pointers.

Greg


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]