I wish the readme example showed a lambda capture. Now that's where compiler magic REALLY shows up.
Here's an example: https://cppinsights.io/s/f04b6896
(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++
// ...
}
“ int foo(int i) {
int add(int x) { return i + x; } // nested function
return add(3);
}
int bar(int i {
alias lambda = (x) => x + i; // lambda
return lambda(3);
}
}