Boost logo

Boost :

From: Marco Costalba (mcostalba_at_[hidden])
Date: 2008-05-13 17:11:50


On Tue, May 13, 2008 at 9:32 PM, Giovanni Piero Deretta
<gpderetta_at_[hidden]> wrote:
>
> // adding all overloads, If I can get it my way :)
> struct foo_fwd {
> template<typename T>
> void operator()(T x) {
> foo(x);
> }
> };
>
> my_msf f = foo_fwd();
> //sizeof(f) = O( sizeof(boost::function) )
>
>
> Comments? Did I convince you? ;)
>
>

Yes ;-) I'll stick to one_copy_only behaviour for set_all()

But because to implement the N copy case is trivially simple (much
simpler then your example) I would like to change the signature of
set_all in the following way

template<typename Fun>
void set_all(Fun const& fun, bool multi_instance = false);

Regarding the sizeof(f) == O( sizeof(boost::function) * N ) problem, given that

- boost::function object are member data, so allocated on the stack

- boost::function object are empty at instantation

- boost::function object are created at MSF instantation (seldom event
or not in critical loop context?), not during assignment (more often
event, more performance critical ? )

I would like to know if

- boost::function default c'tor does some allocation? An empty
boost::function is cheap to create ?

- When a functor is assigned by reference with boost::ref
boost::function just copies a pointer/reference ?

If the above it's true, but I am not an expert of boost::function, the
problem of allocation of N boost::function perhaps is more theoretical
then real, also the size problem, given that boost::function requires
just 3 pointers does not seem a biggie IMHO.

BTW remember the resul_of implementation request?

>// a stateless polymorphic function object:
>struct twice {
> template<class Sig> struct result;
>
>// the result type is simply the argument type,
>// but in principle it can be an
>// arbitrary function of the argument type.
> template<class T> struct result<twice(T)> { typedef T type; };
>
> template<class T>
> T operator()(T f) { return f + f; }
>};

The bad news is that for a polymorphic function object implementation
of struct resut is not trivial, MSF takes a sequence of signatures as
template parameter!

so implementing template<class T> struct result<msf(T)> { ????? }; for
something like

msf<mpl::vector<int(int), char(double, float),
a_class(my_namespace::my_class), void(my_other_class, int, int, int)>

does not seem immediate.

The good news is that I've done it ;-) and the thing I'm proud of is
that implementation does NOT require any metaprogramming stuff dealing
with sequences and with building/splitting and comparing signatures
and even does not require any argument-arity dependant new
specializations apart from what is already present. So no new argument
arity dependant code bloat!!!

It is very simple, I even don't believe it can work...but it does :-)

Marco


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk