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
If the function has an output that cannot be silently discarded, you can
28
29
29
-
## Parameter passing
30
+
31
+
## Parameters
32
+
33
+
TODO
34
+
35
+
> Note: All parameters and other objects in Cpp2 are `const` by default, except for local variables. For details, see [Design note: `const` objects by default](https://github.com/hsutter/cppfront/wiki/Design-note%3A-const-objects-by-default).
36
+
37
+
38
+
## Return values
30
39
31
40
TODO
32
41
33
-
All parameters and other objects in Cpp2 are `const` by default, except for local variables. For details, see [Design note: `const` objects by default](https://github.com/hsutter/cppfront/wiki/Design-note%3A-const-objects-by-default).
42
+
43
+
### Function outputs
44
+
45
+
A function's outputs are its return values, and the "out" state of any `out` and `inout` parameters.
46
+
47
+
Function outputs cannot be silently discarded. To explicitly discard a function output, assign it to `_`. For example:
48
+
49
+
```cpp title="Example: No silent discard"
50
+
f: () -> void = { }
51
+
g: () -> int = { return 10; }
52
+
h: (inout x: int) -> void = { x = 20; }
53
+
54
+
main: ()
55
+
= {
56
+
f(); // ok, no return value
57
+
58
+
std::cout << g(); // ok, use return value
59
+
_ = g(); // ok, explicitly discard return value
60
+
g(); // ERROR, return value is ignored
61
+
62
+
{
63
+
x := 0;
64
+
h( x ); // ok, x is referred to again...
65
+
std::cout << x; // ... here, so its new value is used
66
+
}
67
+
68
+
{
69
+
x := 0;
70
+
h( x ); // ok, x is referred to again...
71
+
_ = x; // ... here, its value explicitly discarded
72
+
}
73
+
74
+
{
75
+
x := 0;
76
+
h( x ); // ERROR, this is a definite last use of x
77
+
} // so x is not referred to again, and its
78
+
// 'out' value can't be implicitly discarded
79
+
}
80
+
```
81
+
82
+
> Cpp2 imbues Cpp1 code with nondiscardable semantics, while staying fully compatible as usual:
83
+
>
84
+
> - A function written in Cpp2 syntax that returns something other than `void` is always compiled to Cpp1 with `[[nodiscard]]`.
85
+
>
86
+
> - A function call written in Cpp2 `x.f()` member call syntax always treats a non-`void` return type as not discardable, even if the function was written in Cpp1 syntax that did not write `[[nodiscard]]`.
0 commit comments