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

后端 未结 7 1926
名媛妹妹
名媛妹妹 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.

提交回复
热议问题