Description
Preface
Cpp2 has the following control structures:
if CONDITION { ... }
else if CONDITION { ... }
...
else { ... }
while CONDITION next STEP { ... }
do { ... }
while CONDITION next STEP;
for ITEMS do: (ITEM) = { ... }
While I always thought that for
loop gets a lambda, and because of that I started to think that Cpp2 should have user-defined language constructs. But NO, hopefully the statement block of for
loop is not a lambda.
All control structures of Cpp2 have statement blocks, therefore we don't need to capture anything inside the statement block of for
loop.
How to reproduce?
It's an example:
#include <iostream>
#include <vector>
#include <string>
main: () = {
items: std::vector<int> = (1, 2, 3);
r: = 10;
for items do: (item) = {
r = 0;
std::cout << item << "\n";
}
}
A typical programmer would think that : (item) = { ... }
is a lambda because of its similarity with lambda syntax, but hopefully it's not. Cpp2 generates the following code:
auto main() -> int{
std::vector<int> items {1, 2, 3};
auto r {10};
for ( auto const& cpp2_range = items; auto const& item : cpp2_range ) {
r = 0;
std::cout << item << "\n";
}
}
Suggestion Detail
I didn't know I should create a bug report or a suggestion for ambiguous problem of for
loop. By the way I created a suggestion.
I suggest to change the syntax of for
loop from:
for ITEMS do: (ITEM) = {
...
}
to something consistent with other control structures such as one of the following syntax:
for ITEM keyword ITEMS {
...
}
for ITEMS keyword ITEM {
}
In which:
keyword
for the first option can bein
,of
,iter
or etc.keyword
for the second option can benext
,each
,loop
or etc.
Any other syntax which is similar to other control structures, is good.
Why do I suggest this change?
Because:
- The statement block of
for
loop syntax: (item) = { ... }
is ambiguous with lambda syntax. A typical programmer expects to capture variables inside it. - The statement block of
for
loop syntax: (item) = { ... }
is inconsistent with other control structures which their statement block are simply{ ... }
.
Your Questions
Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?
No.
Will your feature suggestion automate or eliminate X% of current C++ guidance literature?
Yes absolutely. It would make control structures to be consistent with each other in addition to simplification of guidance:
{ ... }
will be for the statement block of control structures, therefore variables don't have to be captured.- But
: (args) = { ... }
will be for lambdas, therefore variables have to be captured.for
loop breaks this rule. - Expressions in control structures (e.g. the expression after
next
keyword inwhile
loop) won't be evaluate at first evidence, and they can be evaluated multiple times. - But expressions in other places (e.g. for variable initialization or function arguments) will be evaluated one time at first evidence.
Considered alternatives
The other alternative solution is to make the statement block of for
loop to be actually a lambda, which is weird and inconsistent with other control structures.