(variadic) templates and storing pointers to (arbitrary) memberfunctions

M

Michael Haupt

Hi,

I've asked this on the gcc-help mailing list already but was
redirected to this group as the question seemed to be too C++
specific. So, apologies to all that have seen this already.

What I would like to do is create a map<s, f> mapping some symbol type
to functions (the latter are member functions of classes).

My problem is that I am stuck defining the correct type for f. It
should be able to represent member functions of arbitrary classes,
with arbitrary parameter lists. So far, I have something along the
lines of

template<typename T, typename... A> class D {
public:
typedef void (T::*fp)(A...);
};

to define myself a function pointer type:

class MyClass {
public:
typedef D<MyClass>::fp fp;
typedef map<MySymbol*, fp> fmap;
static fmap dmap;
};

The "typedef D" line, however, instantiates the template, binding T to
MyClass and binding (?) A... to "naught".

My understanding of the problem is that what I really want is a
partial instantiation of the template that still leaves the A...
unbound, creating a more generic type.

Is that possible at all?

I have done some research (obviously not enough) and have come to a
vague idea about using std::tr1::mem_fn and bind, but is that correct?
I would really appreciate some advice.

Thanks in advance,

Michael
 
A

Alf P. Steinbach

* Michael Haupt:
Hi,

I've asked this on the gcc-help mailing list already but was
redirected to this group as the question seemed to be too C++
specific. So, apologies to all that have seen this already.

What I would like to do is create a map<s, f> mapping some symbol type
to functions (the latter are member functions of classes).

My problem is that I am stuck defining the correct type for f. It
should be able to represent member functions of arbitrary classes,
with arbitrary parameter lists. So far, I have something along the
lines of

template<typename T, typename... A> class D {
public:
typedef void (T::*fp)(A...);
};

to define myself a function pointer type:

class MyClass {
public:
typedef D<MyClass>::fp fp;
typedef map<MySymbol*, fp> fmap;
static fmap dmap;
};

The "typedef D" line, however, instantiates the template, binding T to
MyClass and binding (?) A... to "naught".

My understanding of the problem is that what I really want is a
partial instantiation of the template that still leaves the A...
unbound, creating a more generic type.

Is that possible at all?

I have done some research (obviously not enough) and have come to a
vague idea about using std::tr1::mem_fn and bind, but is that correct?
I would really appreciate some advice.

Check out boost::bind (go to <url: http://www.boost.org/>).


Cheers & hth.,

- Alf
 
S

SG

I've asked this on the gcc-help mailing list already but was
redirected to this group as the question seemed to be too C++
specific. So, apologies to all that have seen this already.

What I would like to do is create a map<s, f> mapping some symbol type
to functions (the latter are member functions of classes).

My problem is that I am stuck defining the correct type for f. It
should be able to represent member functions of arbitrary classes,
with arbitrary parameter lists.

How would you use such a map (and call the functions?)

iter_t it = map.find("my symbol");
if (it!=map.end()) {
it->second(); // <-- like this?
}
My understanding of the problem is that what I really want is a
partial instantiation of the template that still leaves the A...
unbound, creating a more generic type.

Is that possible at all?

No. Instead, try runtime polymorphism:

map<std::string,std::tr1::function<void()> > map;

You can initialize a function<> object with pretty much any kind of
functor object as long as the "signature is compatible". This includes
functors like bind1st(mem_fun(&T::foo),&t).

Cheers!
SG
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top