Function object bit drop
Hanlray@gmail.com
1, why do you use it?
Function object is a class object that has a modified form of a similar function due to the heavy load operator (). Usually use a function object to implement some form of callback, and the power object has the following advantages over the callback implemented by the traditional function pointer:
Save data between multiple calls. Since the function object can have a member variable, it is easy to save data between multiple calls. The conventional function must be very unnatural with a static partial variable or global variable. Higher running efficiency. For calls that use the function pointer, the compiler cannot determine the name of the actual call, the generated machine instruction is only an indirect call; and the call to the function object, the compiler knows the name of the function (ie overload call call) The function of the operator is therefore possible to generate a directly called machine instruction, and the invocation operator can be invoked to obtain further efficiency, and a function of the function passed through the pointer is very difficult. More flexible. Some operations can be provided in the function object, complete the initialization and extraction work of the relevant data.
2 is best designed to pass value
The reason is that the algorithms that can be used in STL are designed to be delivered. Of course, you can use explicit template to pass reference, but you may bring problems (such as object cutting problems); carefully designed class or function templates may remove these problems, but you can guarantee that you are not used in these templates. STL? If the value is passed, the function object cannot be polymorphic and should be small. And in reality, the object is often not the case. At this time, we need to use C usual method PIMPL: move the data and / or polymorphism of large and / or polymorphic functions to another (implementation class), Then give your function object a pointer to this new class, this requires multiple function object instances to share an implementation class instance, Boost's Shared_Ptr is of course:
Template
3 Boost.Function
Sometimes we need to implement a generalization callback, can call normal functions or function objects, as long as they meet the specified type (return value type, parameter number and type), of course, the template parameters can be used to implement the callback, just like STL's algorithm, but the case of using the class, often needs to turn the class into class template, and the class template must be explicitly instantiated so that the client's code is not simple enough, Boost.Function passes a smart packaging , Unify the type of uniform normal function and function object, and does not lose type security, with strong use value. Boost.Thread uses boost.function: class thread {public: explicit thread (const boost :: function0
Here, the constructor of thread declares that it accepts a number of parameters 0, the return value type is a VOID, whether it is a normal function or a function object. However, the convenience of Boost.Function also has a price. In addition to the increase in the space, the performance efficiency is also reduced, and those performance mad people have to pay attention.
4 boost.bind
Sometimes we want to call back the function or function object does not satisfy the type of call to the callback, just because more than a few parameters that can be determined, it is very convenient to use boost.bind, it is std :: bind1st And std :: bind2nd, not only supporting more parameters, but also the binding method is also flexible, but also supports the binding of function objects and member functions. For example, if you want to use Boost :: Thread to create a thread to perform a function: Void f (INT i), if this is written: boost :: thread thrd (f) is wrong, because the Thread constructor declaration accepts an unparalleled and returned Type of Void, and does not provide the value of parameter i and cannot be run, then write:
Boost :: Thread THRD (Bind (f, 1)) // Assume that it is running F
5 boost.mem_fn
There is a container V of a Shape object, wants to call the Draw method for each object that it is accommodated.
For_each (v.begin (), v.end (), & shape :: draw)
Is not right, because you can't call on the member function and the member function pointer, you must call the member function through an object or object pointer. The function object generated by Boost.mem_fn is to point to the pointer or reference or smart pointer to the member function. It can support multiple parameters, so it is generally generalized in STL.
6 Boost.Lambda
Have a container for an object pointer, what do you do when you want to destroy the objects you reference? Hand writing loop? Turning; use for_each seems to save the troubles of writing cycles, but have to write a function object in another place, it seems to be more troublesome, it seems to spread a partial solution to the outside, and will reduce the code readable Sex. This time Boost.Lambda may be helpful:
For_each (v.begin (), v.end (), bind (delete_ptr (), _1))
By generating a nameless function object for the Lambda expression, Lambda can effectively avoid your worries of simple function objects. For general overloaded operators, usage and in C , such as <<, =, etc .; Operators such as New, Delete, etc., have their corresponding expression (as in the above); the BIND provided by Lambda; even the control structure is supported.
Last Modified: Sunday, March 6th, 2005
HTML Conversion Bytex2Page 2005-02-27