Pointer-to-member as template argument

A

Adam Dziendziel

Hi all!

I'm writing a luabind/boost::python-like binding utility for a Squirrel
language to generating wrapper-functions for C++ classes and have a
problem with passing the pointer-to-member argument and deducing the
type of them.

I want to make a library which I can use like that:

class_<fs::path, sq_filesystem::ttag>(v, "path", no_base)
.constructor<const char*>()
.def("native_file_string", &fs::path::native_file_string)
;

I didn't have problems with contructor - I've just write a specialized
constructor for each type:

// base template
template<typename Class, int TypeTag, typename Sig>
struct sq_constructor {};

// const char* constructor template
template<class Class, int TypeTag>
struct sq_constructor<Class, TypeTag, const char*>
{
static int implementation(HSQUIRRELVM v)
{
if (sq_gettype(v, 2) == OT_STRING)
{
const char * sz;
sq_getstring(v, 2, &sz);
Class * o = new Class(sz);
sq_setinstanceup(v, 1, o);
return 0;
}
else
return sq_throwerror(v, "invalid param - string expected");
};
};

and implement a _class::method:

template<class Class, unsigned int TypeTag>
class class_
{
// adding a constructor
template<class Sig>
inline class_& constructor()
{
sq_register("constructor",
sq_constructor<Class, TypeTag, Sig>::implementation

}

(...)
};

But with creating method wrapper I have a problem. When I wrote:

template<typename Class, int TypeTag, typename Sig, Sig Method>
struct sq_method {};

template<class Class, int TypeTag, std::string (Class::*Method)() const>
struct sq_method<Class, TypeTag, std::string (Class::*)() const, Method>
{
SQUADD_FUNC_PARAMS(0, 1, ".")

static int implementation(HSQUIRRELVM v)
{
Class * o;
SQUADD_SETUP_POINTER(o, 1)

// sq_pushstring(v, o->*Method().c_str(), -1);
return 1;
};
};

template<class Class, unsigned int TypeTag>
class class_
{

// adding a method
template<class Sig>
inline class_& def(const SQChar * name, Sig func)
{
sq_register(name, sq_method<Class, TypeTag, Sig, func>::implementation);

);
};

I had error in class_::def() on template resolution:

`func' is not a valid template argument
error: it must be a pointer-to-member of
the form `&X::Y'

and "`<type error>' is not a class type"

What I'm doing wrong?
 
A

Adam Dziendziel

Thing that I have noticed:

typedef std::string (fs::path::*ftype)(void) const;
SQFUNCTION f;

// works
f = &sq_method<fs::path, 3334, ftype,
&fs::path::native_file_string>::implementation;

// doesn't work
ftype p = &fs::path::native_file_string;
f = &sq_method<fs::path, 3334, ftype, p>::implementation;

GCC 3.3.1 produces:
error: `p' is not a valid template argument
error: it must be a pointer-to-member of the form `&X::Y'

But I must pass the pointer through the .def() method of class_ which
fills the first three template parameters and hide this ugly syntax.
How can I do it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top