Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-01-28 12:28:51

On Monday 28 January 2002 12:03 pm, you wrote:
> As an optimization, it should be possible to detect free and member
> function arguments to the constructor of function<> and treat them as
> though they were passed with ref(), eliminating the need for dynamic
> allocations in many cases.

function<> has this optimization for free functions already.

At one point I tried the same optimization for member function pointers as
well, but ran into problems portably storing member function pointers so that
they could safely be converted back to their original types.

> It would be interesting to know if bind() and
> mem_fn() could be smart enough to report whether they had bound any data
> (i.e. had been used for more than composition),

That would be useful, and I'd be willing to bet that it isn't that hard,

> and even more interesting
> to know if there was some way to detect that they had been bound to
> compile-time constants (I actually don't think that's possible, given that
> all arguments are passed as regular function arguments).

I'd previously convinced myself that this was impossible. Any information to
the contrary would be _very_ well received :)

> One simple approach for function<> to take might be to say that if the
> constructor argument is an empty class or a built-in function or member
> function, it may be treated as though ref() were applied. That wouldn't
> apply the optimization in every possible case, but I think it would in most
> of them.

Two comments here:
  1) Because a class is empty does not necessarily mean that it does not have
state. The constructor/destructor could still perform work, and we'd be
breaking the semantic specification of function<> by eliding the construction
and destruction.
  2) We can't treat such objects as if ref() were applied, because that
assumes that the pointer stored by reference_wrapper<> is valid. We'd end up
performing a dereference operation on an invalid pointer.

I mentioned this form of optimization here (though at this point I
misunderstood your requirements, and ref() was needed):

I still think the answer is the same: add a type trait "is_stateless." For
stateless objects, function<> can just store the type. When calling one of
these objects, function<> will just create a temporary to call through.

Then when bind needs only type information to work,
is_stateless<_bi::bind_t<..> >::value will be true.

> Finally, I thought it might be cute if data member pointers would be
> treated as accessor functions by function/bind/mem_fn. Just a crazy idea...

That might be cute beyond sanity. I think the mem_var_ref adaptor just posted
by Hamish Mackenzie would be appropriate, however.


Boost list run by bdawes at, gregod at, cpdaniel at, john at