Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:10:26+00:00 2026-06-13T10:10:26+00:00

I have a class with two virtual member functions: foo and wrapper . foo

  • 0

I have a class with two virtual member functions: foo and wrapper. foo is short and fast, and wrapper contains a loop that calls foo many times. My hope is that there is some way to inline the calls to foo inside the wrapper function, even when called from a pointer to an object:

MyClass *obj = getObject();
obj->foo(); // As I understand it, this cannot be inlined. That's okay.
obj->wrapper(); // Nor will this. However, I hope that the machine code
                // for the wrapper function will contain inlined calls to
                // foo().

Essentially, I want the compiler to generate multiple versions of the wrapper function — one for each possible class — and inline calls to the appropriate foo, which should be possible since the object type is determined before picking which wrapper function to execute. Is this possible? Do any compilers support this optimization?

Edit: 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.

Edit 2: 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”.

Also, I found an academic paper that proposes an optimization very similar to what I was suggesting: http://www.ebb.org/bkuhn/articles/cpp-opt.pdf.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:10:27+00:00Added an answer on June 13, 2026 at 10:10 am

    In certain cases, compiler can determine the virtual dispatch behavior in compile-time and perform non-virtual function invocation or even inline the function. It can only do that if it can figure out that your class is the “top” in inheritance chain or those two functions are not otherwise overloaded. Oftentimes, this is simply impossible, especially if you don’t have late time optimization enabled for the whole program.

    Unless you want to check the results of your compiler’s optimizations, your best bet would be not to use a virtual function in the inner loop at all. For example, something like this:

    class Foo {
      public:
        virtual void foo()
        {
            foo_impl();
        }
    
        virtual void bar()
        {
            for (int i = 0; i < ∞; ++i) {
                foo_impl();
            }
        }
    
      private:
        void foo_impl() { /* do some nasty stuff here */ }
    };
    

    But in that case you clearly give up the idea that somebody may come in, inherit from your class and throw in their own implementation of “foo” to be called by your “bar”. They will essentially will need to re-implement both.

    On the other hand, it smells a bit like a premature optimization. Modern CPUs will most likely “lock” your loop, predict the exit from it and execute the same µOPs over and over, even if your method is virtually virtual. So I’d recommend you carefully determine this to be a bottleneck before spending your time optimizing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Network class, which I want two virtual functions which I am
I have the following code: A disposable class that contains two disposable members. One
I have two entities like this: public class Service { public virtual int ServiceId
I have a question, here are two classes below: class Base{ public: virtual void
I have a class with two constructors, one that takes no arguments and one
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
So let's say I have two classes that inherit a base class that has
I have two classes: public class Reference { public virtual string Id { get;
I have an MFC C++ program that contains two classes, as follows; struct MyStruct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.