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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:15:21+00:00 2026-05-28T02:15:21+00:00

My dilemma concerns how to best handle long heavy loops which can accept parameters.

  • 0

My dilemma concerns how to best handle long heavy loops which can accept parameters. Consider the following method:

void HeavyLoop(byte* startingAddress, bool secondaryModification)
{
    for (int i = 0; i < 10000000; i++)
    {
        byte* b = startingAddress + i;
        *b+= 1;
        if (secondaryModification) *b+= 2;
    }
}

This method will do what I want, but I am using 10000000 unnecessary ifs inside the loop.

Had I written the same method like this:

void HeavyLoop(byte* startingAddress, bool secondaryModification)
{
    if (secondaryModification)
    {
        for (int i = 0; i < 10000000; i++)
        {
            byte* b = startingAddress + i;
            *b+= 1;
            *b+= 2;
        }
    }
    else
    {
        for (int i = 0; i < 10000000; i++)
        {
            byte* b = startingAddress + i;
            *b+= 1;         
        }
    }   
}

I would get the same result, though my entire loop code would have to be duplicated. This is not a big deal if we’re talking about one parameter, but when you have 4 independent parameters I would have to write 16 different versions of the loop.

What is the “correct” solution in cases like this?
If this were a language like Python I could just dynamically build a function to handle the loop. Is there something similar in C++?

Needless to say, the code is only an example and not the actual case. Please don’t give solutions pertaining to *b+=1 per se.
I am a C++ novice so forgive me if there is a simple solution I am not aware of. Also forgive me if there are syntax errors, I don’t have a compiler handy at the moment.

Edit: The issue is dealing with statements that can not be pre-calculated outside of the loop.

  • 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-28T02:15:22+00:00Added an answer on May 28, 2026 at 2:15 am

    You could implement the loop as a template; the template argument is a compile-time constant, so optimisation ought to remove the unwanted code when it’s false. You then need a wrapper to allow the correct specialisation to be called based on a run-time value:

    template <bool secondaryModification>
    void HeavyLoop(byte* startingAddress)
    {
        for (int i = 0; i < 10000000; i++)
        {
            byte* b = startingAddress + i;
            *b+= 1;
            if (secondaryModification) *b+= 2;
        }
    }
    
    void HeavyLoop(byte* startingAddress, bool secondaryModification)
    {
        if (secondaryModification) {
            HeavyLoop<true>(startingAddress);
        } else {
            HeavyLoop<false>(startingAddress);
        }
    }
    

    During compilation, both versions of the template will be instantiated (one containing *b+=2; and one not, and neither performing a runtime test on the argument); they should then be inlined in the wrapper function to generate exactly the same code as your second example – but without the need to duplicate any source code.

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

Sidebar

Related Questions

Whenever there is dilemma to do a particular task, which can be accomplished either
I have a dilemma, maybe you can help me. I have a table which
I have an interesting dilemma to which, I hope, someone may have an answer.
I have a dilemma that I've encountered before. What's the best in terms of
I have a small dilemma. If you have the following URI endpoints: /item /item/{id}
So here is my dilemma, I want to handle view events on my view
The dilemma: I have options that need to store pricing data. Each option can
I have the following dilemma: I have a list of strings, and I want
Here is my dilemma... I basically have a script which by means of CURL
I have a dilemma, I have two UIPickerViews which show when two distinct views

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.