[bind] Pointer to functions as template parameters

Hi. When using pointers to functions with bind, the particular function doesn't play a part in the return value's type of bind. That is void func1(); void func2(); typeid(boost::bind(&func1)) == typeid(boost::bind(&func1)) // this is true I need the types to be different to store bind objects with the serialization library. The C++ Templates book proposes an adaptor that takes the pointer to function as a template parameter. However, this adaptor is particular to a given signature, and I don't know how to rewrite it more generally. Is this possible? Can I more easily make boost::bind do what I want? Bruno

On Mon, 12 Sep 2005 23:31:52 -0300, Bruno Martínez
Hi.
When using pointers to functions with bind, the particular function doesn't play a part in the return value's type of bind. That is
void func1(); void func2();
typeid(boost::bind(&func1)) == typeid(boost::bind(&func1)) // this is true
I've made some progress. What I have now looks like:
template

On 9/14/05, Bruno Martínez
On Mon, 12 Sep 2005 23:31:52 -0300, Bruno Martínez
wrote: Hi.
When using pointers to functions with bind, the particular function doesn't play a part in the return value's type of bind. That is
void func1(); void func2();
typeid(boost::bind(&func1)) == typeid(boost::bind(&func1)) // this is true
I've made some progress. What I have now looks like:
template
struct nontypeadapt; template
struct nontypeadapt { typedef Ret result_type; Ret operator() () { return func(); } }; then
typeid(boost::bind(nontypeadapt
())) != typeid(boost::bind(nontypeadapt ())) However, the signature has to be given at the call site, it's repetitive to define all specializations of nontypeadapt, and it brings more forwarding problems of it's own. Any ideas?
I think you can only pass functions with external linkage as parameters to templates, which I think it makes it unusable...
Bruno
-- Felipe Magno de Almeida Developer from synergy and Computer Science student from State University of Campinas(UNICAMP). Unicamp: http://www.ic.unicamp.br Synergy: http://www.synergy.com.br "There is no dark side of the moon really. Matter of fact it's all dark."

template
struct nontypeadapt; template
struct nontypeadapt { typedef Ret result_type; Ret operator() () { return func(); } }; then
typeid(boost::bind(nontypeadapt
())) != typeid(boost::bind(nontypeadapt ())) However, the signature has to be given at the call site, it's repetitive to define all specializations of nontypeadapt, and it brings more forwarding problems of it's own. Any ideas?
I think you can only pass functions with external linkage as parameters to templates, which I think it makes it unusable...
I believe that's true, yes, but it's usable nonetheless. The assembler generated by vc71 for a bind with nontypeadapt is better, too. Bruno

Bruno Martínez wrote:
Hi.
When using pointers to functions with bind, the particular function doesn't play a part in the return value's type of bind. That is
void func1(); void func2();
typeid(boost::bind(&func1)) == typeid(boost::bind(&func1)) // this is true
I need the types to be different to store bind objects with the serialization library. The C++ Templates book proposes an adaptor that takes the pointer to function as a template parameter. However, this adaptor is particular to a given signature, and I don't know how to rewrite it more generally. Is this possible? Can I more easily make boost::bind do what I want?
Bruno You may be making more work for yourself than you need to, why do you need to serialize bind objects? I haven't used the serialization lib, but a quick look-through doesn't really bring any times to mind where I would want to serialize a boost::bind functor!

Simon Buchan
Bruno Martínez wrote:
Hi.
When using pointers to functions with bind, the particular function doesn't play a part in the return value's type of bind. That is
void func1(); void func2();
typeid(boost::bind(&func1)) == typeid(boost::bind(&func1)) // this is true
I need the types to be different to store bind objects with the serialization library. The C++ Templates book proposes an adaptor that takes the pointer to function as a template parameter. However, this adaptor is particular to a given signature, and I don't know how to rewrite it more generally. Is this possible? Can I more easily make boost::bind do what I want?
Bruno You may be making more work for yourself than you need to, why do you need to serialize bind objects? I haven't used the serialization lib, but a quick look-through doesn't really bring any times to mind where I would want to serialize a boost::bind functor!
I would like to do the same thing - boost::bind can be used to bind an instance of an object, a member function pointer _and_ any parameters required by that function. This leads to a really useful implementation of the command pattern - you can build a list of these functors which constitute a composite command, and to serialise the whole lot - or part - would be invaluable in all sorts of situations including tracking how much of the composite command had actually been implemented. Maybe I should generalise and boostify my implementation. Any interest? S

On Thu, 15 Sep 2005 10:47:41 -0300, Simon Carter
I would like to do the same thing - boost::bind can be used to bind an instance of an object, a member function pointer _and_ any parameters required by that function.
This leads to a really useful implementation of the command pattern - you can build a list of these functors which constitute a composite command, and to serialise the whole lot - or part - would be invaluable in all sorts of situations including tracking how much of the composite command had actually been implemented.
Maybe I should generalise and boostify my implementation. Any interest?
Yes. Could you explain it's principles? I've been trying to solve the same problem for some time now. The first difficulty is the serialization of a boost::function like class. A possible implementation for such a class is defining an abstract base class and creating by template means the apropiate subclass in the constructor. Boost.Serialization requires registration of all these classes but there's an unbounded number of possible implementations. I've 'solved' this by relying in typeinfo::name which is unportable but works. See a recent thread started by me on this topic. The second problem is serialization of the argument functors themselves. For a user written one, this is easy. For boost::bind it's hard. If the functor adapted by bind is a pointer to function, we are out of luck, as boost::serialization chokes on these. It would be conceivable to have facilities to register pointer to functions, but there aren't any now. boost::bind returns objects of unspecified type, so giving them serialization is hacky at best. They aren't DefaultConstructable. If they were, the visitor facility would be of use (some const_cast would be needed here). Note that boost::function uses a different strategy, in which only pointers to functions are stored inside, not pointers to objects, in order to avoid code bloat. This is where I am now. I'm very interested to learn about your implementation. Regards, Bruno

Bruno Martínez
On Thu, 15 Sep 2005 10:47:41 -0300, Simon Carter
wrote: I would like to do the same thing - boost::bind can be used to bind an instance of an object, a member function pointer _and_ any parameters required by that function.
This leads to a really useful implementation of the command pattern - you can build a list of these functors which constitute a composite command, and to serialise the whole lot - or part - would be invaluable in all sorts of situations including tracking how much of the composite command had actually been implemented.
Maybe I should generalise and boostify my implementation. Any interest?
Yes. Could you explain it's principles? I've been trying to solve the same problem for some time now.
Sorry, perhaps I wasn't clear enough - I haven't solved the general problem of the serialisation of bind or function objects using boost at all - though I would like to do the same thing. The implementation I have is too specialised on persisting just the bits I need, and uses a our own persistence mechanism. I'm afraid it wouldn't be much of a starting point for a general solution, though I too would be very keen to see one.
participants (4)
-
Bruno Martínez
-
Felipe Magno de Almeida
-
Simon Buchan
-
Simon Carter