Gültigkeitsbereiche



  • Hallo.

    Ich kann mir folgende Frage im Standard nicht klar beantworten:

    void Funktion(int x)
    {
      for(int x=0; x < 10; x++) {cout << "Blah";}
    }
    

    Ist das nun nach dem Standard erlaubt und definiert, oder nicht?
    Ich weiß jedenfalls nicht ob die Deklaration für die for-Schleife in den Schleifenblock zugerechnet wird.

    Wurde sicherlich schon einmal durchgenommen, fand aber nichts mittels Suchfunktion.

    Vielen Dank für Eure Hilfe!



  • Ist definiert. Das äussere x wird vom inneren überdeckt.



  • In 3.3.2:4

    Names declared in the for-init-statement, and in the condition of if, while, for, and switch statements are local to the if, while, for, or switch statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for the if statement, any of the outmost blocks) of the controlled statement; see 6.4.

    Also ist es nicht erlaubt außerhalb des for-init-statement den gleichen Namen zu deklarieren?

    Wieso meckert der ICC dann nicht (auch mit /W3)?



  • Drakon: Wird da in der zweiten Zeile nicht das Gegenteil behauptet?



  • Hmm. Ich habe nicht nachgeschaut gehabt und bin davon ausgegangen, dass es, wie sonst mit Überdeckung gehandhabt wird..


  • Mod

    Das ist anders zu verstehen. Es ist gemeint, das sowas nicht erlaubt ist:

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

  • Mod

    sqrt(-1) schrieb:

    In 3.3.2:4

    Names declared in the for-init-statement, and in the condition of if, while, for, and switch statements are local to the if, while, for, or switch statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for the if statement, any of the outmost blocks) of the controlled statement; see 6.4.

    Also ist es nicht erlaubt außerhalb des for-init-statement den gleichen Namen zu deklarieren?

    Wieso meckert der ICC dann nicht (auch mit /W3)?

    Es ist vom "controlled statement" die Rede, also dem Statement, auf das sich das if/while/for/switch bezieht, nicht dem Block, innerhalb dessen das for steht.

    void foo(int x)
    {
      for(int x=0; x < 10; x++) // ok
      {
          int x; // Fehler
          {
              int x;  // ok
          }
      }
    }
    

Anmelden zum Antworten