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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:23:32+00:00 2026-05-30T09:23:32+00:00

Consider below functions f(int a[]) { ///CODE for for if(a[i] > 0) //change i

  • 0

Consider below functions

f(int a[])
{
     ///CODE
     for
      for
       if(a[i] > 0)
       //change i on some condition
     for
        //CODE
        if(a[i] > 0)
    ///CODE

}
f(int a[], int th)
{
     ///CODE
     for
      for
        if(a[i] < th)
       //change i on some condition
     for
        //CODE
        if(a[i] < th)
    ///CODE
}

So we have f function with a optimized huge body,
several lines are the same: if(a[i] > 0)
I want to add extend this function so that if a parameter is added, those lines should change to if(a[i] < th)
When I overload the function, hundreds of line is duplicated (becomes hard to maintain), so i don’t want this. Also I cannot divide the body into functions because “the line” appears in too many inner loops.

First idea:

f(int a[], int th = -1)
{
     ///CODE
        if(th == -1)
            if(a[i] > 0)...
        else
            if(a[i] < th)...
     ///CODE
}

I cannot do this because of the performance overhead of introducing the additional if to the inner loop.
Is there a way to solve it both efficiently and clearly, perhaps using templates or macros?

  • 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-30T09:23:33+00:00Added an answer on May 30, 2026 at 9:23 am

    You can pass functions via template arguments:

    template<bool Cmp(int,int)>
    void f(int a[], int th)
    {
        //..
        if (Cmp(a[i],th)) {
           //...
        }
        //...
    }
    

    You can then use f like this:

    bool cmp(int a, int b) { return a < b; }
    f<cmp>(...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the code below: this.usedIds = 0; this.SendData = function(data) { var id =
Consider the Lua code below: local util = {} function util:foo(p) print (p or
Consider the code below: <?php class Base { protected $name = Base; public function
Let's consider the below example. There, I have: target MAIN calls target t and
Consider the JavaScript code below, inspired from the YUI documentation on YAHOO.lang.extend . In
Consider the Java code below, what would happen if there were no paintComponent method
My questions are divided into three parts Question 1 Consider the below code, #include
I have a doubt in scope of varibles inside anonymous functions in C#. Consider
Consider the below C++ code class B; class A{ private: B* mB; }; class
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>

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.