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 250935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:33:09+00:00 2026-05-11T21:33:09+00:00

I have a C++ application that can be simplified to something like this: class

  • 0

I have a C++ application that can be simplified to something like this:

class AbstractWidget {
 public:
  virtual ~AbstractWidget() {}
  virtual void foo() {}
  virtual void bar() {}
  // (other virtual methods)
};

class WidgetCollection {
 private:
  vector<AbstractWidget*> widgets;

 public:
  void addWidget(AbstractWidget* widget) {
    widgets.push_back(widget);
  }

  void fooAll() {
    for (unsigned int i = 0; i < widgets.size(); i++) {
      widgets[i]->foo();
    }
  }

  void barAll() {
    for (unsigned int i = 0; i < widgets.size(); i++) {
      widgets[i]->bar();
    }
  }

  // (other *All() methods)
};

My application is performance-critical. There are typically thousands of widgets in the collection. The classes derived from AbstractWidget (of which there are dozens) typically leave many of the virtual functions not overridden. The ones that are overridden typically have very fast implementations.

Given this, I feel I can optimize my system with some clever meta-programming. The goal is to leverage function inlining and to avoid virtual function calls, while keeping the code managable. I’ve looked into the Curiously Recurring Template Pattern (see here for description). This seems to almost do what I want, but not quite.

Is there any way to make the CRTP work for me here? Or, is there any other clever solution anyone can think of?

  • 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-05-11T21:33:10+00:00Added an answer on May 11, 2026 at 9:33 pm

    CRTP or compile-time polymorphism is for when you know all of your types at compile time. As long as you’re using addWidget to collect a list of widgets at runtime and as long as fooAll and barAll then have to handle members of that homogenous list of widgets at runtime, you have to be able to handle different types at runtime. So for the problem you’ve presented, I think you’re stuck using runtime polymorphism.

    A standard answer, of course, is to verify that the performance of runtime polymorphism is a problem before you try to avoid it…

    If you really need to avoid runtime polymorpism, then one of the following solutions may work.

    Option 1: Use a compile-time collection of widgets

    If your WidgetCollection’s members are known at compile time, then you can very easily use templates.

    template<typename F>
    void WidgetCollection(F functor)
    {
      functor(widgetA);
      functor(widgetB);
      functor(widgetC);
    }
    
    // Make Foo a functor that's specialized as needed, then...
    
    void FooAll()
    {
      WidgetCollection(Foo);
    }
    

    Option 2: Replace runtime polymorphism with free functions

    class AbstractWidget {
     public:
      virtual AbstractWidget() {}
      // (other virtual methods)
    };
    
    class WidgetCollection {
     private:
      vector<AbstractWidget*> defaultFooableWidgets;
      vector<AbstractWidget*> customFooableWidgets1;
      vector<AbstractWidget*> customFooableWidgets2;      
    
     public:
      void addWidget(AbstractWidget* widget) {
        // decide which FooableWidgets list to push widget onto
      }
    
      void fooAll() {
        for (unsigned int i = 0; i < defaultFooableWidgets.size(); i++) {
          defaultFoo(defaultFooableWidgets[i]);
        }
        for (unsigned int i = 0; i < customFooableWidgets1.size(); i++) {
          customFoo1(customFooableWidgets1[i]);
        }
        for (unsigned int i = 0; i < customFooableWidgets2.size(); i++) {
          customFoo2(customFooableWidgets2[i]);
        }
      }
    };
    

    Ugly, and really not OO. Templates could help with this by reducing the need to list every special case; try something like the following (completely untested), but you’re back to no inlining in this case.

    class AbstractWidget {
     public:
      virtual AbstractWidget() {}
    };
    
    class WidgetCollection {
     private:
      map<void(AbstractWidget*), vector<AbstractWidget*> > fooWidgets;
    
     public:
      template<typename T>
      void addWidget(T* widget) {
        fooWidgets[TemplateSpecializationFunctionGivingWhichFooToUse<widget>()].push_back(widget);
      }
    
      void fooAll() {
        for (map<void(AbstractWidget*), vector<AbstractWidget*> >::const_iterator i = fooWidgets.begin(); i != fooWidgets.end(); i++) {
          for (unsigned int j = 0; j < i->second.size(); j++) {
            (*i->first)(i->second[j]);
          }
        }
      }
    };
    

    Option 3: Eliminate OO

    OO is useful because it helps manage complexity and because it helps maintain stability in the face of change. For the circumstances you seem to be describing – thousands of widgets, whose behavior generally doesn’t change, and whose member methods are very simple – you may not have much complexity or change to manage. If that’s the case, then you may not need OO.

    This solution is the same as runtime polymorphism, except that it requires that you maintain a static list of “virtual” methods and known subclasses (which is not OO) and it lets you replace virtual function calls with a jump table to inlined functions.

    class AbstractWidget {
     public:
      enum WidgetType { CONCRETE_1, CONCRETE_2 };
      WidgetType type;
    };
    
    class WidgetCollection {
     private:
      vector<AbstractWidget*> mWidgets;
    
     public:
      void addWidget(AbstractWidget* widget) {
        widgets.push_back(widget);
      }
    
      void fooAll() {
        for (unsigned int i = 0; i < widgets.size(); i++) {
          switch(widgets[i]->type) {
            // insert handling (such as calls to inline free functions) here
          }
        }
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 180k
  • Answers 180k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use MySQL Events, it was introduced in version 5.1. May 12, 2026 at 4:06 pm
  • Editorial Team
    Editorial Team added an answer Try this: private void Button1_Click(Object sender, EventArgs e ) {… May 12, 2026 at 4:06 pm
  • Editorial Team
    Editorial Team added an answer try this SELECT genre AS result FROM genres WHERE genre… May 12, 2026 at 4:06 pm

Related Questions

I have a C++ Windows application that can be extended by writing C++ plugins,
In a C++ application that can use just about any relational database, what would
I have been a long time C# and .Net developer, and have been playing
I'm working on a multithreaded C++ application that is corrupting the heap. The usual

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.