Note that there are some explanatory texts on larger screens.

plurals
  1. POInline virtual function when called from another virtual function?
    primarykey
    data
    text
    <p>I have a class with two virtual member functions: <code>foo</code> and <code>wrapper</code>. <code>foo</code> is short and fast, and <code>wrapper</code> contains a loop that calls <code>foo</code> many times. My hope is that there is some way to inline the calls to <code>foo</code> <em>inside the wrapper function</em>, even when called from a pointer to an object:</p> <pre><code>MyClass *obj = getObject(); obj-&gt;foo(); // As I understand it, this cannot be inlined. That's okay. obj-&gt;wrapper(); // Nor will this. However, I hope that the machine code // for the wrapper function will contain inlined calls to // foo(). </code></pre> <p><strong>Essentially, I want the compiler to generate multiple versions of the wrapper function -- one for each possible class -- and inline calls to the appropriate <code>foo</code>, which should be possible since the object type is determined before picking which <code>wrapper</code> function to execute.</strong> Is this possible? Do any compilers support this optimization?</p> <p><strong>Edit:</strong> I appreciate all of the feedback and answers so far, and I may end up picking one of them. However, most responses ignore the last part of my question where I explain why I think this optimization should be feasible. That is really the crux of my question and I am still hoping someone can address that.</p> <p><strong>Edit 2</strong>: I picked Vlad's answer since he both suggested the popular workaround and partially addressed my proposed optimization (in the comments of David's answer). Thanks to everyone who wrote an answer -- I read them all and there wasn't a clear "winner".</p> <p>Also, I found an academic paper that proposes an optimization very similar to what I was suggesting: <a href="http://www.ebb.org/bkuhn/articles/cpp-opt.pdf" rel="nofollow">http://www.ebb.org/bkuhn/articles/cpp-opt.pdf</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload