Similarities and differences between C + + callback functions (callback) and imitation functions (functor)

Source: Internet
Author: User

The callback function (callback) and The Imitation function (functor) are often very similar in terms of use, so that we frequently equate them. For example:

inline bool compare(int a, int b)
{
   return a > b;
}
 
struct comparer {
  bool operator()(int a, int b) const {
     return a > b;
  }
};
 
void main()
{
   std::vector<int> vec, vec2;
   std::sort(vec.begin(), vec.end(), compare);
   std::sort(vec2.begin(), vec2.end(), comparer());
}

An imitation function (functor) is called an imitation function because it is a technique that uses certain class objects to support operator () to simulate function invocation effects.

If VEC here, vec2 the contents of these two vectors, then from the results of the execution, the use of the callback function compare is the same as the use of the imitation function comparer.

So, should we use callbacks, or do we use a copy function?

Many people say that with the function of imitation, the callback function is ugly, the code is not much like C + + style.

But in fact, the nature of the problem is not in the code style, The imitation function and callback function have advantages and disadvantages, can not generalize.

The advantages of imitation functions (functor)

My suggestion is that if you can use an imitation function, you should use an imitation function instead of a callback. The reason is:

An imitation function can pass context parameters without trace. The callback technique is usually passed with an additional void* parameter. This is also why most people think callback technology is ugly.

Better performance.

Imitation function technology can get better performance, this is more difficult to understand intuitively. You might say that the callback function is inline, how can the performance be worse than the imitation function? Let's analyze it here. We assume that a function func (for example, the std::sort above) passes a callback function (such as the Compare above), which can be divided into two situations:

Func is an inline function, and relatively simple, the Func call is eventually expanded, and the call to the callback function becomes a normal function call (rather than an indirect call through the function pointer), and if the callback function is simple, it may also be expanded at the same time. In this case, the callback function is the same as the performance of the affine function.

Func is not an inline function, or rather complex to expand (for example, the above std::sort, we know it is a quick sort, the function is unable to expand because of recursion). The callback function is passed as a function pointer and its code cannot be expanded. and The imitation function is different. Although the func itself is complex and cannot be expanded, the invocation of an Func function in the functions of a compiler can be determined and inline expanded during compilation. So in this case, the affine function has better performance than the callback function. Moreover, this performance advantage is sometimes an unparalleled advantage (as for std::sort, because the number of elements being compared is very large and whether an inline expansion can lead to an avalanche effect).

The Imitation function (functor) cannot be done?

Then again, the imitation function does not completely replace all the application situations of the callback function. For example, I used a callback function in Std::autofreealloc rather than an imitation function, because the autofreealloc had to accommodate heterogeneous destructors, rather than just supporting a single kind of destructor. This and template (template) cannot be processed in the same container to support heterogeneous types, is a truth.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.