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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:16:45+00:00 2026-05-20T11:16:45+00:00

I am facing a problem that I cannot see how it is solvable without

  • 0

I am facing a problem that I cannot see how it is solvable without #defines or incurring a performance impact although I am sure that someone can point me to a solution.

I have an algorithm that sort of produces a (large) series of values. For simplicity’s sake, in the following I pretend it’s a for loop in a for loop, although in my code it’s more complex than that.

In the core of the loop I need to do calculations with the values being produced. Although the algorithm for the values stays the same, the calculations vary.

So basically, what I have is:

void normal() {

  // "Algorithm" producing numbers (x and y):    
  for (int x=0 ; x<1000 ; x++) {
  for (int y=0 ; y<1000 ; y++) {
    // Calculation with numbers being produced:
    if ( x+y == 800 && y > 790) {
         std::cout << x << ", " << y << std::endl;
    }
    // end of calculation
  }}
}

So, the only part I need to change is

if ( x+y == 800 && y > 790) {
     std::cout << x << ", " << y << std::endl;
}

So, in order to solve that, I could construct an abstract base class:

class inner_0 {
   public: 
   virtual void call(int x, int y) = 0;
 };

and derive a “callable” class from it:

class inner : public inner_0 {
  public: 
  virtual void call(int x, int y) {
    if ( x+y == 800 && y > 790) {
         std::cout << x << ", " << y << std::endl;
    }
  }
};

I can then pass an instance of the class to the “algorithm” like so:

void O(inner i) {

  for (int x=0 ; x<1000 ; x++) {
  for (int y=0 ; y<1000 ; y++) {
      i.call(x,y);
  }}
}

// somewhere else....

inner I;
O(I);

In my case, I incur a performance hit because there is an indirect call via virtual function table. So I was thinking about a way around it. It’s possible with two #defines:

#define OUTER                           \
        for (int x=0 ; x<1000 ; x++) {  \
        for (int y=0 ; y<1000 ; y++) {  \
             INNER                      \
        }}

//  later...
#define INNER                 \
if (x + y == 800 && y > 790)  \
    std::cout << x << ", " << y << std::endl;

OUTER

While this certainly works, I am not 100% happy with it because I don’t necessarly like #defines.

So, my question: is there a better way for what I want to achieve?

  • 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-20T11:16:45+00:00Added an answer on May 20, 2026 at 11:16 am

    Functors :

    struct Caller
    {
        void operator()(int x, int y)
        {
            if ( x+y == 800 && y > 790)
                std::cout << x << ", " << y << std::endl;
        }   
    };
    
    template <typename T>
    void your_algorithm(T foo)
    {
        for (int x=0 ; x<1000 ; x++)
            for (int y=0 ; y<1000 ; y++)
                foo(x, y);
    }
    
    int main()
    {
        your_algorithm(Caller());
    }
    

    They are the best to pass functions as objects. STL uses them a lot !
    It you want type-safety, you can even use Boost.Function or the C++0x std::function.

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

Sidebar

Related Questions

I'm facing a problem while unit testing my forms. The problem is that data
That was helpful kgiannakakis. I'm facing a problem as below: a = ['zbc','2.3'] for
I am facing a problem where-in I cannot select videos from the photo album
I'm facing a problem that Google couldn't solve yet! I'm trying to store URLs
I'm facing a problem I cannot solve. And I do not know whether it
Currently I am facing the problem that my application is showing long GC times
I am facing problem with an Oracle Query in a .net 2.0 based windows
I am facing a problem with .NET generics. The thing I want to do
I'm facing a problem with IE6. I took the toggle function from this website
I am facing a problem. I would like to localize my action names in

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.