
On Tue, May 13, 2008 at 3:01 PM, Giovanni Piero Deretta <gpderetta@gmail.com> wrote:
In practice MSF acts as N different boost::function objects with a think type based dispatcher. I can of course fix the problem by adding another layer of indirection:
Have you tried to fix it with
msf_function<int(int), double(double), my_big_int(my_big_int)> x;
x = boost::ref( frob(10) );
Indeed boost::function supports ref() and cref() to avoid the copy when is not needed or when the copy is simply wrong. MSF supports ref() and cref() too and forwards them, still wrapped, to the underlying boost::function objects so that no copy of the wrapped polymorphic function object is made.
This has different semantics from what I wanted. I do not want to have to keep the wrapped object in the stack to add it in a wrapper. In this case I need I want MSF to copy my object (i.e. to manage its lifetime) but I want it to do only one copy (usually for efficiency, but sometimes like this case, for correctness).
Actually currently MSF does not copy anything, takes the functor object as const& and forwards to boost::function It's the default implementation of boost::function that takes the functors by value instead of by reference. See point 4 of boost::function FAQ http://www.boost.org/doc/libs/1_35_0/doc/html/function/faq.html If I have understood correctly you would like that wrapping in a boost::ref would be done internally in MSF, something like template<typename F> void set_all(F const& f) { F& stored_object_copy = store_a_copy_somewhere(f); do_set_all(boost::ref( stored_object_copy )); } Is it correct? And in this case what is your suggestion for an API to fallback on the (currently default and more natural at least by an implementation point of view) N copy behaviour? Thanks Marco P.S: BTW I've tested the use of boost::ref() to fix the single state problem: it works nicely and easily ;-)