You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
intmain() { // 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
27
27
```
28
28
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.
30
34
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:
32
36
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"
34
38
#include<iostream>// Cpp1 syntax
35
39
#include<string_view>// Cpp1 syntax
36
40
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
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:
51
45
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
+
```
53
53
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!
0 commit comments