Skip to content

Commit 8f7d265

Browse files
committed
Improve mixed files examples and flow
1 parent eb085c5 commit 8f7d265

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

docs/reference-cppfront.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@ Cppfront compiles a `.cpp2` file and produces a `.cpp` file to be compiled by yo
77

88
The same `.cpp2` file may contain both Cpp2 syntax and today's "Cpp1" C++ syntax, **side by side but not nested**.
99

10-
For example, this is not valid because it tries to nest Cpp2 code inside Cpp1 code, and vice versa:
11-
12-
``` cpp title="ERROR.cpp2 — this is NOT allowed" linenums="1" hl_lines="5 6 9 14"
13-
#include <iostream> // Cpp1 syntax
14-
#include <string_view> // Cpp1 syntax
15-
16-
namespace N { // Cpp1 syntax
17-
hello: (msg: std::string_view) = // Cpp2 syntax (not allowed inside Cpp1 code)
18-
std::cout << "Hello, (msg)$!\n"; // Cpp2 syntax
19-
} // Cpp1 syntax
20-
21-
main: () = { // Cpp2 syntax
22-
auto words = std::vector{ "Alice", "Bob" }; // Cpp1 syntax (not allowed inside Cpp2 code)
23-
N::hello( words[0] ); // ? could be either
24-
N::hello( words[1] ); // ? could be either
25-
std::cout << "... and goodnight\n"; // ? could be either
26-
} // Cpp2 syntax
10+
For example, this source file is fine, where the Cpp2 and Cpp1 code are side by side and seamlessly call each other directly as usual:
11+
12+
``` cpp title="mixed.cpp2 — Mixing Cpp1 and Cpp2 code side by side in the same source file is okay" linenums="1" hl_lines="4-7"
13+
#include <iostream> // Cpp1 syntax
14+
#include <string_view> // Cpp1 syntax
15+
16+
N: namespace = { // Cpp2 syntax
17+
hello: (msg: std::string_view) = // Cpp2 syntax
18+
std::cout << "Hello, (msg)$!\n"; // Cpp2 syntax
19+
} // Cpp2 syntax
20+
21+
int main() { // Cpp1 syntax
22+
auto words = std::vector{ "Alice", "Bob" }; // Cpp1 syntax
23+
N::hello( words[0] ); // Cpp1 syntax
24+
N::hello( words[1] ); // Cpp1 syntax
25+
std::cout << "... and goodnight\n"; // Cpp1 syntax
26+
} // Cpp1 syntax
2727
```
2828

29-
The above nesting is not supported because it would create not just parsing problems but also semantic ambiguities. For example, if lines 11 and 12 are Cpp2, then the `words[0]` and `words[1]` subscript expressions are bounds-checked and bounds-safe by default; but if they are Cpp1 lines, they are not bounds-checked.
29+
When cppfront compiles such a mixed file, it just passes through the Cpp1 code as-is, and translates the Cpp2 code to Cpp1 in-place. This means that when a call site (call this the "caller") uses a type/function/object (call this the "callee") written in the same file:
30+
31+
- **Code written in all Cpp2 is always order-independent by default.** When a caller written in Cpp2 syntax uses a callee written in Cpp2 syntax, they can appear in either order in the file.
32+
33+
- **Code written in Cpp1 is order-dependent as usual.** When either the caller or the callee (or both) are written in Cpp1 syntax, the callee must be declared before the caller.
3034

31-
This is fine, where the Cpp2 and Cpp1 code are side by side and seamlessly call each other directly as usual:
35+
However, this source file is not valid, because it tries to nest Cpp2 code inside Cpp1 code, and vice versa:
3236

33-
``` cpp title="mixed.cpp2 — this is perfectly okay" linenums="1" hl_lines="4-7"
37+
``` cpp title="ERROR.cpp2 — this is NOT allowed" linenums="1" hl_lines="5 6 9 14"
3438
#include <iostream> // Cpp1 syntax
3539
#include <string_view> // Cpp1 syntax
3640

37-
N: namespace = { // Cpp2 syntax
38-
hello: (msg: std::string_view) = // Cpp2 syntax
39-
std::cout << "Hello, (msg)$!\n"; // Cpp2 syntax
40-
} // Cpp2 syntax
41-
42-
int main() { // Cpp1 syntax
43-
auto words = std::vector{ "Alice", "Bob" }; // Cpp1 syntax
44-
N::hello( words[0] ); // Cpp1 syntax
45-
N::hello( words[1] ); // Cpp1 syntax
46-
std::cout << "... and goodnight\n"; // Cpp1 syntax
41+
namespace N { // Cpp1 syntax
42+
hello: (msg: std::string_view) = // Cpp2 syntax (NOT allowed here)
43+
std::cout << "Hello, (msg)$!\n"; // Cpp2 syntax (NOT allowed here)
4744
} // Cpp1 syntax
48-
```
49-
50-
When cppfront compiles such a mixed file, it just passes through the Cpp1 code as-is, and translates the Cpp2 code to Cpp1 in-place. This means that when a call site (call this the "caller") uses a type/function/object (call this the "callee") written in the same file:
5145

52-
- **Code written in all Cpp2 is always order-independent by default.** When a caller written in Cpp2 syntax uses a callee written in Cpp2 syntax, they can appear in either order in the file.
46+
main: () = { // Cpp2 syntax
47+
auto words = std::vector{ "Alice", "Bob" }; // Cpp1 syntax (NOT allowed here)
48+
N::hello( words[0] ); // ?
49+
N::hello( words[1] ); // ?
50+
std::cout << "... and goodnight\n"; // ?
51+
} // Cpp2 syntax
52+
```
5353
54-
- **Code written in Cpp1 is order-dependent as usual.** When either the caller or the callee (or both) are written in Cpp1 syntax, the callee must be declared before the caller.
54+
> The above nesting is not supported because it would create not just parsing problems but also semantic ambiguities. For example, lines 11-13 are syntactically valid as Cpp1 or as Cpp2, but if they are treated as Cpp2 then the `words[0]` and `words[1]` expressions' `std::vector::operator[]` calls are bounds-checked and bounds-safe by default, whereas if they are treated as Cpp1 then they are not bounds-checked. And that's a pretty important difference to be sure about!
5555
5656
5757
## Cppfront command line

0 commit comments

Comments
 (0)