cppfront
cppfront copied to clipboard
trouble trying out vector iteration
I was curious to see if cpp2 improvements included iterator invalidation safety so I tried the following:
#include <vector>
main: () -> int = {
v : std::vector = (1, 2, 3, 4, 5);
i : int = 0;
for (auto& item : v) {
if i > 3 {
v.resize(100);
}
i += 1;
}
}
I got the for loop syntax from other regression tests but cppfront doesn't accept it. It appears to be complaining about imbalanced parens but AFAICT they are balanced. Errors:
~/cppfront/regression-tests$ ../cppfront resize-invalidation-check.cpp2 && g++-10 -fconcepts -std=c++2a -I../include resize-invalidation-check.cpp
resize-invalidation-check.cpp2...
resize-invalidation-check.cpp2(7,21): error: unexpected text - expression-list is not terminated by ) (at ':')
resize-invalidation-check.cpp2(7,23): error: expected valid range expression after 'for' (at 'v')
resize-invalidation-check.cpp2(7,24): error: expected ; at end of statement (at ')')
resize-invalidation-check.cpp2(7,24): error: invalid statement in compound-statement (at ')')
resize-invalidation-check.cpp2(7,24): error: ill-formed initializer (at ')')
resize-invalidation-check.cpp2(3,1): error: unexpected text at end of Cpp2 code section (at 'main')
resize-invalidation-check.cpp2(2,0): error: parse failed for section starting here
resize-invalidation-check.cpp2: In function ‘int main()’:
resize-invalidation-check.cpp2:12:15: error: request for member ‘resize’ in ‘v’, which is of non-class type ‘int’
12 | }
| ^
Also not sure what prevents call to resize... in generated code v is shadowed, not sure if this is incidental or some deliberate attempt to prevent what I'm doing?
The syntax for for loops in cpp2 is different. It should be rewritten to:
#include <vector>
main: () -> int = {
v : std::vector = (1, 2, 3, 4, 5);
i : int = 0;
for v do :(inout item : _) = {
if i > 3 {
v.resize(100);
}
i += 1;
}
}
(https://godbolt.org/z/d8o1bzMWc)
or using next syntax
#include <vector>
main: () -> int = {
v : std::vector = (1, 2, 3, 4, 5);
i : int = 0;
for v next i++ do :(inout item : _) = {
if i > 3 {
v.resize(100);
}
}
}
When cppfront complain about braces you should check regression-tests directory for some examples of syntax. For loops please check:
- https://github.com/hsutter/cppfront/blob/main/regression-tests/pure2-intro-example-three-loops.cpp2
Please remember that in cpp2 sections you can use only cpp2 syntax.
I got the syntax from a cpp2 regression test file already, I copied the last loop here: https://github.com/hsutter/cppfront/blob/main/regression-tests/mixed-intro-example-three-loops.cpp2
Why does it work there but not here?
Seems like a lie. The whole file is Cpp1 syntax. If the function signature is not Cpp2 syntax, the body won't be parsed as Cpp2 syntax, AFAIK.
Please learn the cpp2 syntax from files starting with pure2-. These are the files that have only cpp2 syntax.
@jgarvin wrote:
I was curious to see if cpp2 improvements included iterator invalidation safety
Not yet implemented, but planned. See this project's home page README.md > 2015: Lifetime safety section, which has links to the static analysis specification (part of the C++ Core Guidelines and videos showing a couple of Cpp1 demo implementations.
@JohelEGP wrote:
The whole file is Cpp1 syntax.
Yes, a .cpp2 file can be all today's syntax. That particular file was one of a series of demos that shows gradual migration from Cpp1 syntax to Cpp2 syntax so it started with Cpp2 syntax, so I checked it in when I checked in all my other test/demo files.