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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:27:36+00:00 2026-05-29T15:27:36+00:00

I have an expensive function defined in a base class, which depends on low

  • 0

I have an expensive function defined in a base class, which depends on low level information from its derived classes:

class BaseClass{
 ...
 // Defined in derived class
 virtual int low_level(int)=0;

 // Expensive function depending on the pure virtual function
 void myExpensiveFunction(){
   for(...){
     for(...){
       for(...){
         ... = low_level(...);
         ...
       }
     }
   }
 }
};

class DerivedClass : public BaseClass{
   // A very cheap operation that can be inlined:
   inline virtual int low_level(int i){
     return a[i];
   }

   // Calling the base class function
   void test(){
     myExpensiveFunction();
   }
};

If I understand things correctly, the fact that the low-level function is virtual prevents it from being inlined in the code above. Now, I was thinking about a way to get around this and thought of the following solution, where I pass a pointer to the derived class member function as a template parameter:

class BaseClass{
 ...
 // The function is now templated by the derived function:
 template<typename D, int (D::*low_level)(int)>
 void myExpensiveFunction(){
   for(...){
     for(...){
       for(...){
         ... = static_cast<D*>(this)->low_level(...);
         ...
       }
     }
   }
 }
};

class DerivedClass : public BaseClass{
   // A very cheap operation that can be inlined:
   inline int low_level(int i){
     return a[i];
   }

   // Calling the base class function
   void test(){
     myExpensiveFunction<DerivedClass,&DerivedClass::low_level>();
   }
};

Does this strategy make sense? I imagine that the low level operation will be inlined when the expensive base class function is expanded in the derived class.

I tested implementing it and it compiles and works, but I haven’t seen any noticeable differences in performance.

Kind regards,
Joel

  • 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-29T15:27:39+00:00Added an answer on May 29, 2026 at 3:27 pm

    Passing the function you want to call using a pointer to member to a base class doesn’t really improve over using a virtual function. In fact, I would expect it to make the situation worse. An alternative approach is to use a function object with an inline function call operator and call this. The “normal” approach is to kind of invert the class hierarchy and use the Curiously Recurring Template Pattern: the idea is to create a template which will derive from its template argument. The template argument is expected to provide the customization points, e.g. the function low_level.

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

Sidebar

Related Questions

I have a class in which looking up an instance is expensive, so instances
Suppose you have a simple function, which can get quite expensive for large values:
I have a fairly expensive array calculation (SpectralResponse) which I like to keep to
I have heard that DateTime.Now is very expensive call (from here ) Is GETDATE()
Suppose I have a generator whose __next__() function is somewhat expensive and I want
Let's say I have the following function: def f(): if TESTING: # Run expensive
I'm writing a powMod function which I have to use quite intensively. The starting
I have some expensive function f(x) that I want to only calculate once, but
There is this simple function which I have used with C++ in the past
I have a several very expensive queries which seem to hog resources that seems

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.