Hacker News new | ask | show | jobs
by Someone 63 days ago
> That also shows how C++'s range-based for-loops work, which is similarly some syntactic suger for a fairly mundane for-loop, but details are interesting: unlike a typical for-loop, the loop variable is declared inside the loop body

That’s not true in C++, is it? There, this does not compile

  for(int i = 0; i < 3; ++i)
  {}
  i = i + 1;
(https://cppinsights.io/s/26405687: error: use of undeclared identifier 'i')

https://en.cppreference.com/cpp/language/for:

“A for statement equivalent to:

  {
    init-statement
    while ( condition )
    {
      statement
      expression ;
    }
  }
Except that […]”

and

“While in C names declared in the scope of init-statement and condition can be shadowed in the scope of statement, it is forbidden in C++:

  for (int i = 0;;)
  {
    long i = 1;   // valid C, invalid C++
    // ...
  }
“