How to store the result of boost::bind?

Hi, if I call boost::bind it creates a function object, is that correct? So how can I give this function object a name? That is, presumably I can have an expression like: SomeTypeHere myFunc = boost::bind(...); But what should SomeTypeHere actually be? I've found I can use boost::function but would rather not for performance reasons. Any other options? Thanks, David

On 2/17/06, David Williams
But what should SomeTypeHere actually be? I've found I can use boost::function but would rather not for performance reasons. Any other options?
Are you sure function is too slow? - Function object wrapper size Function object wrappers will be the size of two function pointers plus one function pointer or data pointer (whichever is larger). On common 32-bit platforms, this amounts to 12 bytes per wrapper. Additionally, the function object target will be allocated on the heap. - Copying efficiency Copying function object wrappers may require allocating memory for a copy of the function object target. The default allocator may be replaced with a faster custom allocator or one may choose to allow the function object wrappers to only store function object targets by reference (using ref) if the cost of this cloning becomes prohibitive. - Invocation efficiency With a properly inlining compiler, an invocation of a function object requires one call through a function pointer. If the call is to a free function pointer, an additional call must be made to that function pointer (unless the compiler has very powerful interprocedural analysis). ~ http://www.boost.org/doc/html/function/misc.html#id2699622 The size of a function is roughly the same as storing a smart pointer and a member function pointer. Calling through a function pointer is roughly the same as a virtual function call. I don't think you can get more speed without losing genericity. The only possibility I can think of would be something akin to the mutable-members-in-a-reference-to-temporary trick used in ScopeGuard ( http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm ), but that's very narrowly applicable. ~ Scott

me22 wrote:
On 2/17/06, David Williams
wrote: But what should SomeTypeHere actually be? I've found I can use boost::function but would rather not for performance reasons. Any other options?
Are you sure function is too slow?
- Function object wrapper size Function object wrappers will be the size of two function pointers plus one function pointer or data pointer (whichever is larger). On common 32-bit platforms, this amounts to 12 bytes per wrapper. Additionally, the function object target will be allocated on the heap.
- Copying efficiency Copying function object wrappers may require allocating memory for a copy of the function object target. The default allocator may be replaced with a faster custom allocator or one may choose to allow the function object wrappers to only store function object targets by reference (using ref) if the cost of this cloning becomes prohibitive.
- Invocation efficiency With a properly inlining compiler, an invocation of a function object requires one call through a function pointer. If the call is to a free function pointer, an additional call must be made to that function pointer (unless the compiler has very powerful interprocedural analysis). ~ http://www.boost.org/doc/html/function/misc.html#id2699622
The size of a function is roughly the same as storing a smart pointer and a member function pointer. Calling through a function pointer is roughly the same as a virtual function call. I don't think you can get more speed without losing genericity.
The only possibility I can think of would be something akin to the mutable-members-in-a-reference-to-temporary trick used in ScopeGuard ( http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm ), but that's very narrowly applicable.
~ Scott
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hmmm... yes I agree the overhead of boost::function is very low (and it's also the most elegant approach) but it does appear to make a difference as I am using it to call a very low cost method (an accessor for a class). But I think this probably is a design error on my part. I think the best approach is for me to adjust my code to use it in a more appropriate way. Thanks anyway! David

David Williams wrote:
Hi, if I call boost::bind it creates a function object, is that correct? So how can I give this function object a name? That is, presumably I can have an expression like:
SomeTypeHere myFunc = boost::bind(...);
But what should SomeTypeHere actually be? I've found I can use boost::function but would rather not for performance reasons. Any other options?
If you want to store it for later use, boost::function<> is your only real option. In principle, you could write int f = boost::bind( ... ); and then copy the real type from the error message, but I wouldn't go that way. :-) If you just need a temporary named copy, you can introduce a helper function templatized on the function object type (an algorithm) and pass boost::bind( ... ) to it. Hopefully in a four years or so the language will allow us to write auto f = boost::bind( ... );
participants (3)
-
David Williams
-
me22
-
Peter Dimov