Is this call to a function object inlined?

Posted by dehmann on Stack Overflow See other posts from Stack Overflow or by dehmann
Published on 2010-05-01T05:51:20Z Indexed on 2010/05/01 11:07 UTC
Read the original article Hit count: 294

Filed under:
|
|
|

In the following code, Foo::add calls a function via a function object:

struct Plus {
  inline int operator()(int x, int y) const {
    return x + y;
  }
};

template<class Fct>
struct Foo {
  Fct fct;
  Foo(Fct f) : fct(f) {}
  inline int add(int x, int y) {
    return fct(x,y); // same efficiency adding directly?
  }  
};

Is this the same efficiency as calling x+y directly in Foo::add? In other words, does the compiler typically directly replace fct(x,y) with the actual call, inlining the code, when compiling with optimizations enabled?

© Stack Overflow or respective owner

Related posts about c++

Related posts about compiler