views:

150

answers:

2

What will happen if I use a inline function inside a virtual function? I'm confused with questions like http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html#faq-31.6

I can understand it, but is that mean, it will non-sense to use (call) inline functions inside virtual functions (please assume that it is calling dynamically)?

class Wrapper
{
public:
   inline void doInlineJob() {;}
};

class Base
{
   virtual void foo()
   {
     //Do something
   }
};

class Derived: public Base
{
    void foo()
    {
      wrapObj.doInlineJob();
    }

    Wrapper wrapObj;
};
+9  A: 

It doesn't matter whether foo is virtual or not. It only matters whether doInlineJob is virtual. It's not, so it can be inlined without a problem.

sepp2k
ahh...That what I wanted to know for sure. Otherwise there aren't use of inline in OOP world at all. Bcz inline functions may get wrapped by virtual functions and then become useless.
Morpheus
+2  A: 

What will happen if I use a inline function inside a virtual function?

Nothing special. If the compiler agrees, it will be inlined, if not, it won't. Just like from any other function.

(Note that the FAQ talks about functions which are themselves inline and virtual at the same time. That's different.)

sbi
Hey!That doInlineJob() is very critical in my context (actually it is pixel access function in a image processing system). And I have use virtual (in OOP context) to manage my system. So, if that inline(ness) in doInlineJob() is going to ignore, it will cause to do some damage to the performance.
Morpheus
Whether `foo()` is `virtual` or not doesn't matter. Inlining is an attribute of the _called_ function not of the calling. Also, `inline` (or implicit inlining by defining member functions within their class' definition) is a hint to the compiler that you think inlining is good. Compilers need not to obey that (just imagine marking a recursive function `inline`) and more often than not they know better than you know. (Heavy inlining could lead to code bloat, thereby increasing your working set and thus actually _slow down_ your application.) Measure first, only then tweak, then measure again.
sbi
Thank you very much for the reply. "Inlining is an attribute of the called function" that was the thing I wanted to know. btw, __forceinline in VC++ is also just a HINT ? (I know there are rules even when they going to put that __forceinline in action)
Morpheus
Measure first, only then tweak, then measure again. yeah...true :)
Morpheus
@Morpheus: I would have to look up `__forceinline`, but from its name I guess it would force the compiler to inline a function, if it can at all. (You still cannot truly inline recursive functions, for example, although recursion could be transformed to iterations and that be inlined.) However, I would stay away from `__forceinline` as long as measurements don't show it to be an improvement in a specific situation. From what I know VC is pretty good in deciding what to inline and what not (and will also inline functions that weren't marked so). Rely on it until it's proven wrong. Double check.
sbi