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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:20:08+00:00 2026-05-11T17:20:08+00:00

You can define 2 variables of the same type in a for loop: int

  • 0

You can define 2 variables of the same type in a for loop:

int main() {
  for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

But it is illegal to define variables of different types:

int main() {
  for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) {
    cout << j << endl;
  }
}

Is there a way to do this? (I don’t need to use i inside the loop, just j.)

If you have totally hacked and obscure solution, It’s OK for me.

In this contrived example I know you could just use double for both variables. I’m looking for a general answer.

Please do not suggest to move any of the variables outside of for body, probably not usable for me as one is an iterator that has to disappear just after the loop and the for statement is to be enclosed in my foreach macro:

#define foreach(var, iter, instr) {                  \
    typeof(iter) var##IT = iter;                     \
    typeof(iter)::Element var = *var##IT;            \
    for (; var##_iterIT.is_still_ok(); ++var##IT, var = *var#IT) {  \
      instr;                                         \
    }                                                \
  }

It can be used thus:

foreach(ii, collection, {
  cout << ii;
}). 

But I need something that will be used like that:

foreach(ii, collection)
  cout << ii;

Please do not introduce any runtime overhead (but it might be slow to compile).

  • 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-11T17:20:08+00:00Added an answer on May 11, 2026 at 5:20 pm

    Here is a version using boost preprocessor (This is just for fun. For the real-world answer, see @kitchen’s one above):

    FOR((int i = 0)(int j = 0.0), i < 10, (i += 1, j = 2 * i)) { 
    
    }
    

    The first part specifies a sequence of declarations: (a)(b).... The variables declared later can refer to variables declared before them. The second and third part are as usual. Where commas occur in the second and third parts, parentheses can be used to prevent them to separate macro arguments.

    There are two tricks known to me used to declare variables that are later visible in a compound statement added outside a macro. The first uses conditions, like an if:

    if(int k = 0) ; else COMPOUND_STATEMENT
    

    Then k is visible. Naturally, it always have to evaluate to false. So it can’t be used by us. The other context is this one:

    for(int k = 0; ...; ...) COMPOUND_STATEMENT
    

    That’s what i’m going to use here. We’ll have to watch to only make one iteration of COMPOUND_STATEMENT. The actual for loop that does the increment and condition checking has to come at the end, so the appended compound statement appertains to it.

    #include <boost/preprocessor.hpp>
    #include <iostream>
    
    #define EMIT_DEC_(R,D,DEC) \
        for(DEC; !_k; ) 
    
    #define FOR(DECS, COND, INC) \
        if(bool _k = false) ; else \
          BOOST_PP_SEQ_FOR_EACH(EMIT_DEC_, DECS, DECS) \
            for(_k = true; COND; INC)
    
    int main() {
        FOR((int i = 0)(float j = 0.0f), i < 10, (i += 1, j = 2 * i)) {
            std::cout << j << std::endl;
        }
    }
    

    It’s creating a bunch of for statements, each nested into another one. It expands into:

    if(bool _k = false) ; else
      for(int i = 0; !_k; )
        for(float j = 0.0f; !_k; )
          for(_k = true; i < 10; (i += 1, j = 2 * i)) {
            std::cout << j << std::endl;
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 92k
  • Answers 92k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer IMO, the best use of Service Broker is for connecting… May 11, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer First, start sed in quiet mode: it should not print… May 11, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer edit: also, shouldn't $p = p.$source; be $p = $p.$source;… May 11, 2026 at 6:25 pm

Related Questions

I wrote a small PHP application that I'd like to distribute. I'm looking for
How would one create a deterministic Javascript HTML colour picker which given arguments of
I like how Java has a Map where you can define the types of
let me tell you a bit about where this question came from. I have
Objective-C has no namespaces; it's much like C, everything is within one global namespace.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.