In C++ why can't I write a for() loop like this: for( int i = 1, double i2 = 0;

后端 未结 7 1899
名媛妹妹
名媛妹妹 2020-12-03 20:49

or, \"Declaring multiple variables in a for loop ist verboten\" ?!

My original code was

 for( int i = 1, int i2 = 1; 
      i2 < mid;
      i++, i         


        
相关标签:
7条回答
  • 2020-12-03 21:21

    Well, I did some more googling, and I think the answer for C++ is "for() statements are very special places" Ick.

    Excerpting from an ISO spec:

    for ( for-init-statement conditionopt ; expressionopt ) statement
    

    where

    for-init-statement:
    expression-statement
    simple-declaration
    

    and they have to specify that

    [Note: a for-init-statement ends with a semicolon. ] 
    

    So the C++ syntax spec. is specifically hacked so that only one decl-spec (i.e. type) is allowed in the first slot. Looks like our attempts to argue from basic principles were doomed. Thanks for all the responses.

    0 讨论(0)
  • 2020-12-03 21:22

    As long as you can write a valid statement with the comma , operator, it's acceptable.

    0 讨论(0)
  • 2020-12-03 21:32

    If you need to use several variables of different type in for-loop then you could use structures as follows:

    for( struct {int i; long i2;} x = {1, 1}; x.i2 < mid; x.i++, x.i2 = x.i * x.i )
    {
      cout << x.i2 << endl;
    }
    

    so this is not a limitation, just use a little different syntax.

    0 讨论(0)
  • 2020-12-03 21:35

    I can see why you hope that would work, but---given that even using the rather simple minded teaching tool that

    for (i=0; i<max; i++){
       ...
    }
    

    is equivalent to

    i=0;
    while (i<max){
       ...
       i++;
    }
    

    you syntax doesn't work---I can't see why you expected that it would. EAch of the bits need to be valid.

    0 讨论(0)
  • 2020-12-03 21:36

    int i = 1, double i2 = 0; is not a valid declaration statement, so it cannot be used inside the for statement. If the statement can't stand alone outside the for, then it can't be used inside the for statement.

    Edit: Regarding your questions about comma operators, options 'A' and 'B' are identical and are both valid. Option 'C' is also valid, but will probably not do what you would expect. z will be assigned 1, and the statements 3 and 4 don't actually do anything (your compiler will probably warn you about "statements with no effect" and optimize them away).

    Update: To address the questions in your edit, here is how the C++ spec (Sec 6.5) defines for:

    for ( for-init-statement condition(opt) ; expression(opt) ) statement
    

    It further defines for-init-statement as either expression-statement or simple-declaration. Both condition and expression are optional.

    The for-init-statement can be anything that is a valid expression-statement (such as i = 0;) or simple-declaration (such as int i = 0;). The statement int i = 1, double i2 = 0; is not a valid simple-declaration according to the spec, so it is not valid to use with for. For reference, a simple-declaration is defined (in Section 7) as:

    attribute-specifier(opt) decl-specifier-seq(opt) init-declarator-list(opt) ;
    

    where decl-specifier-seq would be the data type plus keywords like static or extern and init-declarator-list would be a comma-separated list of declarators and their optional initializers. Attempting to put more than one data type in the same simple-declaration essentially places a decl-specifier-seq where the compiler expects a init-declarator-list. Seeing this element out of place causes the compiler to treat the line as ill-formed.

    The spec also notes that the for loop is equivalent to:

    {
        for-init-statement
        while ( condition ) {
            statement
            expression ;
        }
    }
    

    where condition defaults to "true" if it is omitted. Thinking about this "expanded" form may be helpful in determining whether a given syntax may be used with a for loop.

    0 讨论(0)
  • 2020-12-03 21:37

    C++ (also C and Java) do not permit the declaration of more than one type of variables in the scope of a for loop. In the grammar, that is because the comma does not start a new statement in that context. Effectively, only one declaration is allowed inside the for(;;) statement. The rationale is because that requirement is fairly unusual, and you can get it only with a slightly more verbose construct.

    0 讨论(0)
提交回复
热议问题