Is there a way to define variables of two different types in a for loop initializer?

后端 未结 13 741
余生分开走
余生分开走 2020-12-19 06:12

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 << e         


        
13条回答
  •  没有蜡笔的小新
    2020-12-19 06:56

    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 
    #include 
    
    #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;
          }
    

提交回复
热议问题