1

I read C++ coding standards (Herb Sutter and Andrei Alexandrescu) and two articles below.

Intuitively, a function object has additional overhead compared to a function, because it is an object in C++. However, it seems that function object invocation usually outperforms function calling. I know that inlining is one of the optimizations function object provides.

So my questions are:

  1. Why are function objects so fast? Is it all because of inlining?

  2. How does the function object get implemented? Is it translated into ordinary function call at compile time? Or are there any difference in runtime logic?

  3. Are there any cases in which function objects are slower than functions?

Detailed difference between functor's call and function call?

When do you use function objects in C++?

Community
  • 1
  • 1
Yuu Jinnai
  • 35
  • 5
  • Its basically all in the inlining and other optimalization it enables. – Xarn Aug 18 '14 at 11:06
  • 4
    *"Intuitively, a function object has additional overhead compared to a function, because it is an object in C++"* I don't understand this reasoning. Why should an object be slow? – dyp Aug 18 '14 at 11:11
  • 1
    ***One* question per question, please.** – Lightness Races in Orbit Aug 18 '14 at 11:12
  • Of course, the comparison is with a function *pointer*, not a function. Calling a function indirectly (i.e. via a pointer) can't usually be inlined, and may well be slower than a direct call to a function object's member function even if neither is inlined. – Mike Seymour Aug 18 '14 at 11:22
  • C++ object does not create any additional overhead On the low-level class method is just a function with additional hidden parameter (this), unless it's virtual, of course –  Aug 18 '14 at 11:31
  • Thanks for linking to the useful article. So seems the answer is that it would all translated in compile time. I thought there are overhead for calling function object. And I finally got what the inlining actually means. http://stackoverflow.com/questions/13722426/why-can-lambdas-be-better-optimized-by-the-compiler-than-plain-functions – Yuu Jinnai Aug 18 '14 at 12:08

0 Answers0