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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:29:45+00:00 2026-05-22T01:29:45+00:00

I want to write code that compiles conditionally and according to the following two

  • 0

I want to write code that compiles conditionally and according to the following two cases:

CASE_A:

    for(int i = 1; i <= 10; ++i){
        // do something...
    }

CASE_B: ( == !CASE_A)

    {
        const int i = 0;
        // do something...
    }

That is, in case A, I want to have a normal loop over variable i but, in case B, i want to restrict local scope variable i to only a special case (designated here as i = 0). Obviously, I could write something along the lines:

    for(int i = (CASE_A ? 1 : 0); i <= (CASE_A ? 10 : 0); ++i){
         // do something
    }

However, I do not like this design as it doesn’t allow me to take advantage of the const declaration in the special case B. Such declaration would presumably allow for lots of optimization as the body of this loop benefits greatly from a potential compile-time replacement of i by its constant value.

Looking forward to any tips from the community on how to efficiently achieve this.

Thank you!

EDITS:

CASE_A vs CASE_B can be evaluated at compile-time.

i is not passed as reference

i is not re-evaluated in the body (otherwise const would not make sense), but I am not sure the compiler will go through the effort to certify that

  • 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-22T01:29:46+00:00Added an answer on May 22, 2026 at 1:29 am

    Assuming, you aren’t over-simplifying your example, it shouldn’t matter. Assuming CASE_A can be evaluated at compile-time, the code:

    for( int i = 0; i <= 0; ++i ) {
        do_something_with( i );
    }
    

    is going to generate the same machine code as:

    const int i = 0;
    do_something_with( i );
    

    for any decent compiler (with optimization turned on, of course).

    In researching this, I find there is a fine point here. If i gets passed to a function via a pointer or reference, the compiler can’t assume it doesn’t change. This is true even if the pointer or reference is const! (Since the const can be cast away in the function.)

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

Sidebar

Related Questions

I want to write code that compiles easily for either 32 or 64 bit
I want to write an HttpHandler that compiles CoffeeScript code on-the-fly and sends the
The code that I want to write is like this: void MethodOnThreadA() { for
I often find I want to write code something like this in C#, but
I am a AS3 novice learning PureMVC and want to write code following best
I'm overriding WndProc, so I want to write code like if (m.Msg == WM_COMMAND)
Is there a better way to write this code? I want to show a
I want to write a command that specifies the word under the cursor in
I want to write preprocessor functions/arrays that are evaluated at compile time. For example,
I want to write some library code to be shared across products and across

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.